views:

21

answers:

2

I need code example please.i tried selectedindexchange but it doesnot register any index change what to use?

its c# vs08 asp.net sql server

the code files are

.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {





    }
    protected void Button1_Click(object sender, EventArgs e)
    {//not this
        ///Label3.Text = "clicked clicked clicked";


    }
    protected void Button1_Click1(object sender, EventArgs e)
    {

        Label5.Text = "the tool tip of the button clicked is! HELP!!!";


        //here code please how to which button is clicked?
        //there are many records so?
        //even if i try to use the button id directly
        //it does not appear
        //to vs the button does not exist outside the datalist control
        //help

    }
}

the source file

    <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
        ConnectionString="<%$ ConnectionStrings:test1 %>" 
        DeleteCommand="DELETE FROM [1] WHERE [ID] = @ID" 
        InsertCommand="INSERT INTO [1] ([ID], [NAME]) VALUES (@ID, @NAME)" 
        SelectCommand="SELECT * FROM [1]" 
        UpdateCommand="UPDATE [1] SET [NAME] = @NAME WHERE [ID] = @ID">
        <DeleteParameters>
            <asp:Parameter Name="ID" Type="Decimal" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="NAME" Type="String" />
            <asp:Parameter Name="ID" Type="Decimal" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="ID" Type="Decimal" />
            <asp:Parameter Name="NAME" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>
<br />
    <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
    <br />
    <asp:DataList ID="DataList2" runat="server" DataKeyField="ID" 
        DataSourceID="SqlDataSource3">
        <ItemTemplate>
            ID:
            <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
            <br />
            NAME:
            <asp:Label ID="NAMELabel" runat="server" Text='<%# Eval("NAME") %>' />
            <br />
            <br />
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
            -<asp:Label ID="Label2" runat="server" Text='<%# Eval("NAME") %>'></asp:Label>
            &nbsp;
            <br />
            <br />
            <br />
            &nbsp;<asp:Label ID="Label4" runat="server" Text='<%# Eval("ID") %>' 
                ToolTip='<%# Eval("NAME") %>'></asp:Label>
            <br />
            here extra information/ description is binded to tool tip.<br />
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click1" 
                Text='<%# Eval("ID") %>' ToolTip='<%# Eval("NAME") %>' />
            <br />
            when clicked, the text of the button is displayed in the label. but many records 
            so button belonging to which record clicked?<br />
            <br />
            <br />
            <hr />
            <br />
            <br />
        </ItemTemplate>
    </asp:DataList>
    <br />

 






EDIT

<asp:DataList ID="DataList2" runat="server" DataKeyField="ID" 
        DataSourceID="SqlDataSource3">
        <ItemTemplate>
            ID:
            <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
            <br />
            NAME:
            <asp:Label ID="NAMELabel" runat="server" Text='<%# Eval("NAME") %>' />
            <br />
            <br />
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
            -<asp:Label ID="Label2" runat="server" Text='<%# Eval("NAME") %>'></asp:Label>
            &nbsp;
            <br />
            <br />
            <br />
            &nbsp;<asp:Label ID="Label4" runat="server" Text='<%# Eval("ID") %>' 
                ToolTip='<%# Eval("NAME") %>'></asp:Label>
            <br />
            here extra information/ description is binded to tool tip.<br />
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click1" 
                Text='<%# Eval("ID") %>' ToolTip='<%# Eval("NAME") %>' />
            <br />
            when clicked, the text of the button is displayed in the label. <br />
            <br />
            <br />
            <asp:Button ID="Button2" runat="server" CommandArgument='<%# Eval("NAME") %>' 
                CommandName="Explain" Text='<%# Eval("ID") %>' />
            <asp:TextBox ID="TextBox1" runat="server">First Record</asp:TextBox>
            <br />
            when clicked takes argument from button and the text in the text box, displayed. 
            (record 1)<br />
            <br />
            <br />
            <br />
            <asp:Button ID="Button3" runat="server" CommandArgument='<%# Eval("NAME") %>' 
                CommandName="Explain" Text='<%# Eval("ID") %>' />
            //<br />
            when clicked does the same as above
            <br />
            <hr />
            <br />
            <br />
        </ItemTemplate>
    </asp:DataList>

code behind

protected void DataList2_ItemCommand(object sender, DataListCommandEventArgs e) { // all of the buttons within the row that have the CommandName property set can cause this event handler to execute. // Use the CommandName argument to determine which button was clicked and take the appropriate action switch (e.CommandName) {

        case "Explain":
            // update your label using the command argument rather that the button's ToolTip
            Label5.Text = e.CommandArgument.ToString();

            TextBox TextBox1 = e.Item.FindControl("TextBox1") as TextBox;

            Label6.Text = TextBox1.Text;

            break;



        default:
            Label5.Text="ERROR";
            break;
    }
}

mistake:- i forgot to put OnItemCommand="MyDataList_ItemCommand" in datalist source code ...

+2  A: 

You could do this:

protected void Button1_Click1(object sender, EventArgs e)
{
    Label5.Text = (sender as Button).ToolTip;
}

Also, if you know that you want to work with other controls within that row, you could use the DataList.ItemCommand event instead of the Button.Click event. Below is an example of how you might do that:

ASP Markup:

        <asp:Label ID="MyLabel" runat="server" />
        <asp:DataList ID="MyDataList" runat="server" OnItemCommand="MyDataList_ItemCommand">
            <ItemTemplate>
                <!-- Suppose you had some input controls that you needed to work with as well -->
                <asp:TextBox ID="txtInput1" runat="server" />
                <asp:TextBox ID="txtInput2" runat="server" />
                <asp:Button ID="btnMyCommand" runat="server" CommandName="MyCommand" CommandArgument='<%# Eval("NAME") %>' Text='<%# "Execute My Command on ID:" + Eval("ID") %>' ToolTip='<%# string.Format("This will execute the \"My Command\" command on {0}.", Eval("NAME")) %>' />
                <!-- just some examples of other buttons on the same row that execute different commands -->
                <asp:Button ID="btnDoSomethingCrazy" runat="server" CommandName="Do Something Crazy!" Text="Do Something Crazy!" />
                <asp:LinkButton ID="btnEdit" runat="server" CommandName="Edit" Text="Edit" />
            </ItemTemplate>
        </asp:DataList>

Code-Behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack && !Page.IsCallback)
        {
            // some example data
            MyDataList.DataSource = new[] {
                new { ID = 1, NAME = "ABCD" },
                new { ID = 2, NAME = "BCDE" },
                new { ID = 3, NAME = "CDEF" },
            };
            MyDataList.DataBind();
        }
    }

    protected void MyDataList_ItemCommand(object sender, DataListCommandEventArgs e)
    {
        // all of the buttons within the row can cause this event handler to execute.
        // Use the CommandName argument (populated by the CommandName property of the button that was clicked) in order to determine which button was clicked and take the appropriate action
        switch (e.CommandName)
        {
            case "Edit":
                // ...
                break;
            case "Update":
                // ...
                break;
            case "Cancel":
                // ...
                break;
            case "Delete":
                // ...
                break;
            case "MyCommand":
                // update your label using the command argument rather that the button's ToolTip
                MyLabel.Text = e.CommandArgument.ToString();

                TextBox txtInput1 = e.Item.FindControl("txtInput1") as TextBox;
                TextBox txtInput2 = e.Item.FindControl("txtInput2") as TextBox;

                string value1 = txtInput1.Text;
                string value2 = txtInput2.Text;

                // do something with the input values
                break;
            case "Do Something Crazy!":
                // ...
                break;
        }
    }
Dr. Wily's Apprentice
+1 for beating me by 1/100th of a second!
Matthew Jones
forgot to declare the event....OnItemCommand="MyDataList_ItemCommand"thanks
for other new bees the following can also be done by setting the itemcommand property of the datalist from design view to the method made with the name datalist1_blahblah.... NOTE:-the method will appear in the dropdown list if its there in the .cs file
there may a mistake in " // all of the buttons within the row that have the CommandName property set can cause this event handler to execute." even those which donot have this set, like in the above editted code of question the "button1 click" cause the click event and the itemcommand event of datalist to execute!.. why is that?
Hrm, you are correct. The documentation for the DataList.ItemCommand event (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.itemcommand.aspx) just says that it "occurs when any button is clicked in the DataList control," and that it "is commonly used when you have a button control with a custom CommandName value." I will update the comments in my example to be more accurate. As far as why...the control designers probably just didn't see a reason to restrict the event to buttons with CommandName set. If CommandName is null, you can just choose to ignore it.
Dr. Wily's Apprentice
+1  A: 

You can try casting the sender:

protected void Button1_Click1(object sender, EventArgs e)
{
    Button myButton = (Button)sender;
    Label5.Text = myButton.ToolTip;
}
Matthew Jones