ASP.NET从datalist

时间:2016-10-24 13:22:45

标签: c# asp.net

我的ASP.Net网络表单中有一个数据列表。 datalist有一个图像按钮和一个超链接。这些东西保存在datalist的item模板中。我想获取单击图像的行的id。这是我的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>

我有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());

        }*/
    }

需要帮助。 感谢

这是我绑定我的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();

1 个答案:

答案 0 :(得分:0)

你试过哪个命令被解雇了吗?检查一下,请告诉我结果。

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

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

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


            }
        }