我在页面上放置了一个GridView控件,然后在page_load事件里通过编程为GridView绑定了数据然后在CS文件里写了一个方法 protected string getId()
{
string pid = "";
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chkBox = row.FindControl("CheckBox1") as CheckBox;
if (chkBox.Checked)
{
if (pid.Equals(""))
pid = GridView1.DataKeys[row.RowIndex].Value.ToString();
else
pid +=","+GridView1.DataKeys[row.RowIndex].Value.ToString();
}
}
return pid;
}
这个方法在page_load事件中的ispostback中可以获取到GridView所有选择行的主键值,然而在按钮的点击事件里却一直获取不到,这个原因一直很令我郁闷,请教各位大侠了,GridView里的选择列是通过添加一个模板列,然后在模板项中加入一个CheckBox而形成的截图如下:
{
string pid = "";
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chkBox = row.FindControl("CheckBox1") as CheckBox;
if (chkBox.Checked)
{
if (pid.Equals(""))
pid = GridView1.DataKeys[row.RowIndex].Value.ToString();
else
pid +=","+GridView1.DataKeys[row.RowIndex].Value.ToString();
}
}
return pid;
}
这个方法在page_load事件中的ispostback中可以获取到GridView所有选择行的主键值,然而在按钮的点击事件里却一直获取不到,这个原因一直很令我郁闷,请教各位大侠了,GridView里的选择列是通过添加一个模板列,然后在模板项中加入一个CheckBox而形成的截图如下:
{
string pid = "";
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chkBox = row.FindControl("CheckBox1") as CheckBox;
if (chkBox.Checked)
{
if (pid.Equals(""))
pid = GridView1.DataKeys[row.RowIndex].Value.ToString();
else
pid +=","+GridView1.DataKeys[row.RowIndex].Value.ToString();
}
}
return pid;
}
又是写在什么事件里的叱?
{
if (IsPostBack)
{
string IdList = this.GetID();
Response.Write(IdList); //可以获取主键值并显示出来
}
this.GetData();
} /// <summary>
/// 为GridView绑定数据
/// </summary>
protected void GetData()
{
this.GridView1.DataSource = Manage.BLL.VisitGroups(3);
this.GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string IdList = this.GetID();
Response.Write(IdList); //显示出来的是空白的
} /// <summary>
/// 获取GridView中的主键行
/// </summary>
/// <returns></returns>
protected string getId()
{
string pid = "";
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chkBox = row.FindControl("CheckBox1") as CheckBox;
if (chkBox.Checked)
{
if (pid.Equals(""))
pid = GridView1.DataKeys[row.RowIndex].Value.ToString();
else
pid += "," + GridView1.DataKeys[row.RowIndex].Value.ToString();
}
}
return pid;
}
{
string IdList = this.GetID();
Response.Write(IdList); //显示出来的是空白的
}
问题出在this引用上,在按钮事件中,this引用的是sender.也就是button,怎么可以获取GetID呢
而在protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string IdList = this.GetID();
Response.Write(IdList); //可以获取主键值并显示出来
}
this.GetData();
}中,sender为page 对象,是可以获取的.
楼主要获得IDlist何不用hiddenfield储存呢,string pid = "";
然后,取值的时候要判断rowtype
If r.RowType = DataControlRowType.DataRow ThenIf CType(r.FindControl("chkRow"), CheckBox).Checked = True ThenMsgBox(GridView1.DataKeys(r.RowIndex).Values.Item(0))i = i + 1MsgBox(CType(r.FindControl("lnkedit"), LinkButton).CommandArgument)End IfEnd IfNextGetchkValue()If (MsgBox(i & " records will be delete !", MsgBoxStyle.YesNo) = MsgBoxResult.Ok) ThenTrace.Write(i & " Records deleted")
End IfEnd Sub
js:
var elems = document.getElementById("<%= Yourgridview.ClientID %>").getElementsByTagName("input");
for(var i=0;i<elems.length;i++){
if(elems[i].type=="checkbox")elems[i].checked = true;
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
{
string strTempValue = string.Empty;
foreach (GridViewRow gvr in gv.Rows)
{
CheckBox cb = (CheckBox)gvr.FindControl(checkBoxId);
if (cb.Checked)
{
strTempValue = strTempValue + "," + ((HiddenField)gvr.FindControl(hiddenFieldId)).Value;
}
}
return strTempValue;
}
问题就再ispostback