问了多遍,高手也没解决。
有一个GridView,CheckBox是用模版列放在gridview中的,其中第一列(CheckBox)是模板列,绑定于ID,第0列是ID列是隐藏的。我想在选择checkbox时,将gridview中某一行,某一列的值取出来,赋值给几个TEXTBOX。就这么简单。
问了多次,高手也没解决。希望尽快结帖。请高手的高手帮忙,请附原代码。
我也将前台代码附上
<asp:GridView ID= "GridView1 " runat= "server " AllowPaging= "True " AutoGenerateColumns= "False "
BackColor= "White " BorderColor= "#CCCCCC " BorderStyle= "None " BorderWidth= "1px "
CellPadding= "3 " DataKeyNames= "id " DataSourceID= "SqlDataSource2 " Style= "border-right: #ccccff thin groove;
border-top: #ccccff thin groove; left: -3px; border-left: #ccccff thin groove;
width: 585px; border-bottom: #ccccff thin groove; position: relative; top: -13px;
background-color: #e8e0ef " Width= "315px " OnDataBound= "GridView1_DataBound ">
<FooterStyle BackColor= "White " ForeColor= "#000066 " />
<Columns>
<asp:BoundField DataField= "id " HeaderText= "id " InsertVisible= "False " ReadOnly= "True "
SortExpression= "id " Visible= "False " />
<asp:TemplateField HeaderText= "选择 " SortExpression= "id ">
<EditItemTemplate>
<asp:Label ID= "Label1 " runat= "server " Text= ' <%# Eval( "id ") %> '> </asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID= "CheckBox1 " runat= "server " Style= "position: relative " />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField= "keyid " HeaderText= "编号 " SortExpression= "keyid " />
<asp:BoundField DataField= "CompanyName " HeaderText= "公司名称 " SortExpression= "CompanyName " />
<asp:BoundField DataField= "Department " HeaderText= "部门 " SortExpression= "Department " />
<asp:BoundField DataField= "UserName " HeaderText= "操作员 " SortExpression= "UserName " />
<asp:BoundField DataField= "AddTime " HeaderText= "时间 " SortExpression= "AddTime " />
<asp:BoundField DataField= "Memory " HeaderText= "备注 " SortExpression= "Memory " Visible= "False " />
</Columns>
<RowStyle ForeColor= "#000066 " />
<SelectedRowStyle BackColor= "#669999 " Font-Bold= "True " ForeColor= "White " />
<PagerStyle BackColor= "White " ForeColor= "#000066 " HorizontalAlign= "Left " />
<HeaderStyle BackColor= "#006699 " Font-Bold= "True " ForeColor= "White " />
</asp:GridView>
[解决办法]
不是高手,给个思路。
没用过2005里的GridView,给不出具体代码。
给CheckBox添加OnCheckedChanged事件
<asp:CheckBox ID= "CheckBox1 " runat= "server " Style= "position: relative " OnCheckedChanged= "Check_Clicked " />
点击时响应事件
void Check_Clicked(Object sender, EventArgs e)
{
//这里遍历GridView的行,从中找出Check_Clicked被选中的行,
//然后出你需要的那个列的值就OK
//这在03里很容易实现
}
这样下来第0列和模板列里的label都不需要。
[解决办法]
模板列绑定时
<input id= "chkID " onclick= "ChooseValue(this); " runat= "server " value= ' <%#Eval( "id ")%> ' type= "checkbox " />
<input id= "hdn <%#Eval( "id ")%> " type= "hidden " value= "你要取的值 "/>
js
function ChooseValue(obj)
{
var idx = document.getElementById(obj).value;
//你要取到的值
document.getElementById( 'hdn '+idx).value;
}
[解决办法]
在编辑模板里双击checkbok,进入事件,然后用findControl打到,你想取什么值,怎么做都行了,checkbox的 AutoPostBack= "true " ;
protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
{
CheckBox box;
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
box = this.GridView1.Rows[i].FindControl( "CheckBox3 ") as CheckBox;
if (box.Checked)
{
Response.Write(GridView1.Rows[i].Cells[0].Text);
}
}
}