我的gridview中有一个下拉列表。下拉列表中的数据为“Y”和“N”。这两个字母。如果我选择Y,那么单元格值6将需要变为零。但它不起作用。这是我的代码。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList DropDownList3=(DropDownList)e.Row.FindControl("DropDownList");
//if (DropDownList3 == null)
//if (DropDownList3.SelectedValue == "Y")
if(DropDownList3.SelectedItem.Text == "Y")
{
e.Row.Cells[6].Text = "0";
}
else
{
e.Row.Cells[6].Text = "1";
}
}
}
这是我的dropdowlist模板字段
<asp:TemplateField HeaderText="Alt">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" >
<asp:ListItem>Y</asp:ListItem>
<asp:ListItem>N</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" >
<asp:ListItem>Y</asp:ListItem>
<asp:ListItem>N</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" >
<asp:ListItem>Y</asp:ListItem>
<asp:ListItem>N</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
答案 0 :(得分:2)
首先,您必须为 final int [] finalArr={5,6,8};
System.out.println("Value at index 0 is "+finalArr[0]);
//output : Value at index 0 is 5
//perfectly fine
finalArr[0]=41;
System.out.println("Changed value at index 0 is "+finalArr[0]);
//Changed value at index 0 is 41
int[] anotherArr={7,9,6};
// finalArr=anotherArr;
//error : cannot assign a value to final variable finalArr
设置AutoPostBack="true"
,并且您必须为ddl创建EventHandler,但在DropDownList3
中不在RowCreated
中。
有例子(它的vb.net):
ASPX:
RowDataBound
然后,代码背后:
<asp:TemplateField HeaderText="Alt">
<ItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true">
<asp:ListItem>Y</asp:ListItem>
<asp:ListItem>N</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
非常重要在Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grid1.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ddl As DropDownList = CType(e.Row.FindControl("DropDownList3"), DropDownList)
AddHandler ddl.SelectedIndexChanged, AddressOf ddlChange
End If
End Sub
Private Sub ddlChange(ByVal sender As Object, ByVal e As EventArgs)
Dim ddl As DropDownList = DirectCast(sender, DropDownList)
Dim row As GridViewRow = ddl.NamingContainer
If ddl.SelectedItem.Text = "Y" Then
row.Cells(6).Text = "0"
Else
' do what You want
End If
End Sub
之后你不会绑定GridView
,当然,PostBack
必须设置为EnableViewState
由于我在True
工作,我会尝试将此代码转换为vb.net
。
更新:
有c#
代码(我使用在线转换器):
c#