无法在按钮上获取选定的网格行值单击MVC,C#

时间:2013-04-22 21:57:40

标签: c# asp.net 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>

1 个答案:

答案 0 :(得分: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个复选框,则至少会将数据附加到字符串中并用“ - ”分隔。您可以根据需要随意更改分隔符。