Jump to content

Button Response

Recommended Posts

Hi. I have two buttons that will run the update command on a datalist. I'm looking to find out which button was pushed to activate an Update command on a datalist.The code as I would do it would look something like this (probably not the best coding): protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e) { if (ButtonA Was Clicked) { string CommentsID = DataList1.DataKeys[e.Item.ItemIndex].ToString(); string Author = ((TextBox)e.Item.FindControl("TextBox3")).Text; CommentsDataSource.UpdateParameters["CommentsID"].DefaultValue = CommentsID; CommentsDataSource.UpdateParameters["Author"].DefaultValue = Author; } else //Button B { string CommentsID = DataList1.DataKeys[e.Item.ItemIndex].ToString(); string Author = "Some Other Text"; CommentsDataSource.UpdateParameters["CommentsID"].DefaultValue = CommentsID; CommentsDataSource.UpdateParameters["Author"].DefaultValue = Author; } CommentsDataSource.Update(); DataList1.EditItemIndex = -1; DataList1.DataBind(); }What code gives me the sending button? Thanks in advance.

Link to post
Share on other sites

This may get you going. Its a complete aspx page (no code behind) just for this example. You cast the sender as the object you expect and then query its ID. This may open up all kinds of ideas to you

<%@ page language="C#" %><script runat="server">		protected void Page_Load(object sender, EventArgs e)	{			}	protected void button_Click(object sender, EventArgs e)	{		string sButtonId = ((System.Web.UI.WebControls.Button) sender).ID;		switch (sButtonId)		{			case "button1":				text1.Text = "button1 was pressed";				break;			case "button2":				text1.Text = "button2 was pressed";				break;		}						}</script><html><body><form id="Form1" method="post" runat="server">	<asp:button runat="server" Text="button1" id="button1" OnClick="button_Click"  />	<asp:button runat="server" Text="button2" id="button2" OnClick="button_Click"/>	<asp:TextBox runat="server" ID="text1" /></form></body></html>

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...