In this article i will explain that how can we show TextBox on selection of radio button in gridview.
In this article two columns will display in gridview. In one column radio button is showing and in second column Textbox will show. But i want that when user select radio button then TextBox should show otherwise it should invisible. So below i will explain how can we accomplish this.
following is the javascript function that will show textbox.
<script>
function ShowText(radio, textBox) {
var RadioButton = document.getElementById(radio);
if (RadioButton.checked == true) {
document.getElementById(textBox).style.visibility = "visible";
}
}
</script>
following is html code to show gridview on page
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None"
onrowdatabound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="aRadioButton" runat="server" Text="test" Checked="false"/>
<asp:Label ID="Label1" runat="server" Text='Show'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
following method is page_load event that will fill the gridview. In this i am adding only one columns value for just explaination.
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
for (int i = 0; i < 10; i++)
{
dt.Rows.Add();
dt.Rows[i]["ID"] = i;
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
folloing method will add the onclick attribute to radio button. It will set calling of javascript function.
this is rowdatabound method of gridview so for each row of gridview it will set this functionality.
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButton aRadioButton = (RadioButton)e.Row.FindControl("aRadioButton");
TextBox textBox1 = (TextBox)e.Row.FindControl("TextBox3");
aRadioButton.Attributes.Add("onClick", "JavaScript:ShowText('" + aRadioButton.ClientID + "','" + textBox1.ClientID + "')");
}
}
after doing above all methods and javascript function we will run the application and we will get the desire output.
I hope it will usefull to you.
In this article two columns will display in gridview. In one column radio button is showing and in second column Textbox will show. But i want that when user select radio button then TextBox should show otherwise it should invisible. So below i will explain how can we accomplish this.
following is the javascript function that will show textbox.
<script>
function ShowText(radio, textBox) {
var RadioButton = document.getElementById(radio);
if (RadioButton.checked == true) {
document.getElementById(textBox).style.visibility = "visible";
}
}
</script>
following is html code to show gridview on page
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None"
onrowdatabound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="aRadioButton" runat="server" Text="test" Checked="false"/>
<asp:Label ID="Label1" runat="server" Text='Show'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
following method is page_load event that will fill the gridview. In this i am adding only one columns value for just explaination.
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
for (int i = 0; i < 10; i++)
{
dt.Rows.Add();
dt.Rows[i]["ID"] = i;
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
folloing method will add the onclick attribute to radio button. It will set calling of javascript function.
this is rowdatabound method of gridview so for each row of gridview it will set this functionality.
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButton aRadioButton = (RadioButton)e.Row.FindControl("aRadioButton");
TextBox textBox1 = (TextBox)e.Row.FindControl("TextBox3");
aRadioButton.Attributes.Add("onClick", "JavaScript:ShowText('" + aRadioButton.ClientID + "','" + textBox1.ClientID + "')");
}
}
after doing above all methods and javascript function we will run the application and we will get the desire output.
I hope it will usefull to you.