2
當單擊它或單擊按鈕時,我試圖獲取特定GridView行中的值。 問題是,當我點擊行時什麼都沒有發生,即使當我點擊按鈕時也沒有發生。 行不可點擊。 任何幫助無法獲取按鈕上選定的Gridrow值單擊MVC,C#
這裏我的代碼爲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>
你確定這些行正在用你期望的ID進行渲染嗎? ''選擇$''似乎有點冒失,期望它包含該字符串,也許控制行上的ClientID是你需要的,因爲我做了Web窗體,這就是爲什麼我問了一段時間? – 2013-04-23 07:28:42