Jump to content

Button Response


kirk
 Share

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 comment
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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...