Checkbox in a Gridview

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,

I have been playing with the check box in my gridview. When a user clicks
on this box I just want it to do the following: textbox1.text = "Hello"

I have played with the rowdatabound event but this event occurs when before
the gridview is populated. I also tried the selectindex changed event when I
had the select field invisible. Still no luck with either events.

Can anyone help,
Michael
 
Hi Michael,

I'm not sure if I fully understand your question. But here's my try anyway:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}

private void BindData()
{
grid1.DataSource = new int[5];
grid1.DataBind();
}

protected void check1_CheckedChanged(object sender, EventArgs e)
{
text1.Text = "Hello";
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="text1" runat="server"></asp:TextBox>
<asp:GridView ID="grid1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="check1" runat="server"
AutoPostBack="true" Text="Check me"
OnCheckedChanged="check1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>


Let me know if this is not what you wanted.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Sure. Here's the VB.NET version:

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
If Not IsPostBack Then
BindData()
End If
End Sub

Private Sub BindData()
grid1.DataSource = New Integer(5) {}
grid1.DataBind()
End Sub

Protected Sub check1_CheckedChanged(ByVal sender As Object, ByVal e As
EventArgs)
text1.Text = "Hello"
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="text1" runat="server"></asp:TextBox>
<asp:GridView ID="grid1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="check1" runat="server"
AutoPostBack="true" Text="Check me"
OnCheckedChanged="check1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

</div>
</form>
</body>
</html>


Let me know if this works for you.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Michael,

How about this VB.NET version? Does that answer your question?

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top