JavaScript访问GridView的绑定列(Access the Bound field of Gridview by javascript)
<form id="form1" runat="server">
<asp:GridView ID="GridView1" AutoGenerateColumns="False" ShowHeader="True" runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField HeaderText="Name" DataField="Name" ReadOnly="true" />
<asp:BoundField HeaderText="Amount" DataField="Amount" ReadOnly="true" />
<asp:TemplateField HeaderText="CheckBox">
<ItemTemplate>
<asp:CheckBox ID="gridCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TextBox">
<ItemTemplate>
<asp:TextBox ID="gridTextBox" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
<asp:GridView ID="GridView1" AutoGenerateColumns="False" ShowHeader="True" runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField HeaderText="Name" DataField="Name" ReadOnly="true" />
<asp:BoundField HeaderText="Amount" DataField="Amount" ReadOnly="true" />
<asp:TemplateField HeaderText="CheckBox">
<ItemTemplate>
<asp:CheckBox ID="gridCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TextBox">
<ItemTemplate>
<asp:TextBox ID="gridTextBox" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
<script type="text/javascript">
<!--
function gridCheckBoxOnClick(elementRef, textBoxId, amountValue)
{
document.getElementById(textBoxId).value = (elementRef.checked) ? amountValue : '';
}
// -->
</script>
<!--
function gridCheckBoxOnClick(elementRef, textBoxId, amountValue)
{
document.getElementById(textBoxId).value = (elementRef.checked) ? amountValue : '';
}
// -->
</script>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ( e.Row.RowType == DataControlRowType.DataRow )
{
// The Name is in bound column #0
//string nameValue = e.Row.Cells[0].Text;
// The Amount is in bound column #1
string amountValue = e.Row.Cells[1].Text;
// CheckBox with an ID of "gridCheckBox" is in column #2 of the Grid
CheckBox gridCheckBox = (CheckBox)e.Row.Cells[2].FindControl("gridCheckBox");
// TextBox with an ID of "gridTextBox" is in column #3 of the Grid
TextBox gridTextBox = (TextBox)e.Row.Cells[3].FindControl("gridTextBox");
if ( (gridCheckBox != null) && (gridTextBox != null) )
{
gridCheckBox.Attributes.Add("onclick", string.Format("gridCheckBoxOnClick(this, '{0}', '{1}');", gridTextBox.ClientID, amountValue));
}
}
}
{
if ( e.Row.RowType == DataControlRowType.DataRow )
{
// The Name is in bound column #0
//string nameValue = e.Row.Cells[0].Text;
// The Amount is in bound column #1
string amountValue = e.Row.Cells[1].Text;
// CheckBox with an ID of "gridCheckBox" is in column #2 of the Grid
CheckBox gridCheckBox = (CheckBox)e.Row.Cells[2].FindControl("gridCheckBox");
// TextBox with an ID of "gridTextBox" is in column #3 of the Grid
TextBox gridTextBox = (TextBox)e.Row.Cells[3].FindControl("gridTextBox");
if ( (gridCheckBox != null) && (gridTextBox != null) )
{
gridCheckBox.Attributes.Add("onclick", string.Format("gridCheckBoxOnClick(this, '{0}', '{1}');", gridTextBox.ClientID, amountValue));
}
}
}