简体   繁体   English

ASP.NET从数据列表中查找选定行的ID

[英]ASP.NET Find id of selected row from datalist

I have a datalist in my ASP.Net web form. 我的ASP.Net Web表单中有一个数据列表。 The datalist has an image button and a hyperlink. 数据列表具有图像按钮和超链接。 These things are kept in the item template in the datalist. 这些内容保存在数据列表的项目模板中。 I want to get the id of the row in which the image is clicked. 我想获取单击图像的行的ID。 Here is my ASP.Net code 这是我的ASP.Net代码

 <div height="100%" width="100%" class="w3-center"> <asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand" OnSelectedIndexChanged="DataList1_SelectedIndexChanged1" > <HeaderTemplate></HeaderTemplate> <ItemTemplate> <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%#Eval("Image_path") %> ' CommandName="Item" CommandArguement='<%#Eval("Catg_Id") %>' Height="200" Width="350" NavigateUrl='<%#String.Concat("SelectedCatg.aspx?category=",Eval("Catg_Name")) %>'/><br /> <asp:HyperLink runat="server" NavigateUrl='<%#String.Concat("SelectedCatg.aspx?category=",Eval("Catg_Name")) %>' CssClass="link" Font-Size="18"><%#Eval("Catg_name") %></asp:HyperLink> </ItemTemplate> <FooterTemplate></FooterTemplate> </asp:DataList> </div> 

I have itemCommand event but it doesn't catch the event on clicking the image button. 我有itemCommand事件,但是单击图像按钮时没有捕获该事件。

 protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);

        String index=e.CommandArgument.ToString();
        Response.Redirect("~/SelectedCatg.aspx?id=" + index);
       /* if (e.CommandName.Equals("img"))
        {
            int AnswerId = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());

        }*/
    }

Need help. 需要帮忙。 Thanks 谢谢

This is how I bind my datalist 这就是我绑定数据列表的方式

         String conString = ConfigurationManager.ConnectionStrings["con"].ToString();
         con = new SqlConnection(conString);
         con.Open();
         String query = "Select * from Category_Master";
         cmd = new SqlCommand(query,con);
         dr = cmd.ExecuteReader();
         dt = new DataTable();
         dt.Load(dr);
         DataList1.DataSource = dt;
         DataList1.DataBind();

did you try which command was fired? 您是否尝试过触发哪个命令? check this and let me know the result please. 检查一下,让我知道结果。

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Item") 
            {

                int index= Convert.ToInt32(e.CommandArgument); 

               Response.Redirect("~/SelectedCatg.aspx?id=" + index);


            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM