Asp.Net set Visibility of Dropdown list inside gridview in aTemplate Field
I have a Gridview with Template field
<asp:TemplateField HeaderText="Scoring">
<ItemTemplate>
<asp:DropDownList ID="ddlY_N_NA" runat="server"
开发者_开发百科 Visible='<%#Eval("IsTextBox")%>' ></asp:DropDownList>
<asp:TextBox ID="txtAudit" runat="server"
Visible='<%#Eval("IsTextBox")%>' ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
I need to set the visibility of drop down based on the visibility of Textbox. Either of the two has to be shown per row but not both. Can any help me in this.
Thanks in advance.
Try the following:
<asp:TemplateField HeaderText="Scoring">
<ItemTemplate>
<asp:DropDownList ID="ddlY_N_NA" runat="server"
Visible='<%# ((bool)Eval("IsTextBox")) ? "false" : "true" %>' >
</asp:DropDownList>
<asp:TextBox ID="txtAudit" runat="server"
Visible='<%#Eval("IsTextBox")%>' ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
You can do it with the RowDataBound
event of the Gridview.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow dr = ((DataRowView)e.Row.DataItem).Row;
if (dr["ColumnName"].ToString()) // your Condition
{
((DropDownList)e.Row.FindControl("dropdownID")).Visible = false;
}
else if (dr["ColumnName"].ToString()) // your Condition
{
((TextBox)e.Row.FindControl("TextboxID")).Visible = false;
}
}
}
Hi, Akram Shahda below is my solution
<asp:TemplateField HeaderText="Scoring" HeaderStyle-Width="12%">
<ItemTemplate>
<asp:DropDownList ID="ddlY_N_NA" Visible='<%#SetVisibility(DataBinder.Eval(Container.DataItem,"IsTextBox"))%>'
runat="server" CssClass="Qdropdown">
</asp:DropDownList>
<asp:TextBox onkeypress="return isNumberKey(event);" ID="txtAudit" Visible='<%#Convert.ToBoolean(Eval("IsTextBox"))%>'
MaxLength="10" runat="server" CssClass="Qinputbox" Width="54px" ValidationGroup="txt"></asp:TextBox>
<asp:HiddenField ID="hdnTextBoxCondition" Value='<%#SetTextBoxVisibility(Eval("IsTextBox"),Eval("TextBoxConditions"))%>'
Visible='<%#Eval("IsTextBox")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
In the code behind I have written the methods which will set the visibility
public bool SetVisibility(object value)
{
if (Convert.ToBoolean(value))
return false;
else
return true;
}
public string SetTextBoxVisibility(object value, object condition)
{
if (Convert.ToBoolean(value))
return Convert.ToString(condition);
else
return "";
}
精彩评论