2

我试图在单击它或单击按钮时获取特定gridview行中的值。问题是当我单击该行时没有任何反应,即使我单击按钮也没有。这些行是不可点击的。任何帮助

这是我的 ASPx 页面代码。

       <%@ Page Language="C#"Inherits="System.Web.Mvc.ViewPage<MvcApplication3.Models.Order>" %>

  protected void Page_Load(object sender, EventArgs e)
 {
    GridView1.DataSource = ViewData["list"];
    GridView1.DataBind();
 }

 protected void Button1_Click(object sender, EventArgs e)
 {


    CheckBox x;
    foreach (DataGridItem di in GridView1.Rows)
    {
        x = (CheckBox)di.FindControl("checkBox1");
        if (x.Checked == true)
        {
            Label5.Text = GridView1.SelectedRow.Cells[2].Text;
        }
    }
 }

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
 {
    Label5.Text = string.Format("YOu selected row{0} with {1} {2}",
                               GridView1.SelectedIndex + 1,
                               GridView1.SelectedRow.Cells[0].Text,
                               GridView1.SelectedRow.Cells[1].Text);
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes.Add("ondblclick", "__doPostBack('GridView1','Select$" +      e.Row.RowIndex + "');");
     } 
 }
</script>


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>Index</title>
</head>
<body>
  <form id="form1" runat="server">
 <div>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        onselectedindexchanged="GridView1_SelectedIndexChanged" 
        onrowdatabound="GridView1_RowDataBound">
        <Columns>
            <asp:TemplateField HeaderText="Tillverkning" InsertVisible="False">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%#    Bind("TillverkningsOrder") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Datum" InsertVisible="False">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("OrderDatum")     %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Antal" InsertVisible="False">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Kundnamn" InsertVisible="False">
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("KundNamn")           %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowSelectButton="True" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    <br />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>

 </div>
 </form>
 </body>
 </html>
4

1 回答 1

0

丽娜,

虽然我没有你的问题的答案,但我发现了一个数据一致性问题。

当您滚动复选框时,您每次都在输入 Label5.Text。因此滚动是没有意义的,因为 Label5 只会显示它发现已被勾选的最后一个 Checkbox 的值。

我可以建议以下替代方案:

    CheckBox x;
        foreach (DataGridItem di in GridView1.Rows)
        {
            x = (CheckBox)di.FindControl("checkBox1");
            if (x.Checked == true)
            {
                if(Label5.Text == "")
                {Label5.Text = GridView1.SelectedRow.Cells[2].Text;} 
                else {Label5.Text = Label5.Text + " - " +  GridView1.SelectedRow.Cells[2].Text;}
            }
        }

如果选择了更多 1 个复选框,这至少会将数据附加到用“ - ”分隔的字符串上。随意根据需要更改分隔符。

于 2013-10-19T13:59:24.083 回答