主题:  DataGrid问题

huanhuan_5d

职务:普通成员
等级:1
金币:0.0
发贴:5
#12004/11/16 10:14:29
我的问题和他的差不多:http://www.5d.cn/bbs/NewsDetail.asp?GroupName=ASP%BC%B0.net&dp=10&lp=1&id=1308051


hotinfo

huanhuan_5d

职务:普通成员
等级:1
金币:0.0
发贴:5
#22004/11/16 10:17:12
原代码:
<!-- #Include File="../../db_connection.aspx" -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Language" content="zh-cn">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>查询数据</title>
</head>
<script language="c#" runat="server">
void Page_Load(Object sender,EventArgs e)
{
Db_Start();
string sql_str="select * from C_Log order by id desc";
SqlDataAdapter cmd=new SqlDataAdapter(sql_str,conn);
DataSet ds=new DataSet();
cmd.Fill(ds);
grid1.DataSource=ds;
grid1.DataBind();
conn.Close();
if (!IsPostBack)
{
//添加项目
ArrayList myList = new ArrayList();
int i=1;
string str_shu="";
for (i=1;i<=grid1.PageCount;i++)
{
str_shu=i.ToString();
myList.Add (str_shu);
}
find_page.DataSource = myList;
find_page.DataBind();
}
lblCurrentIndex.Text = "当前页是:" + (grid1.CurrentPageIndex+1);
lblPageCount.Text = "共 " + grid1.PageCount+" 页";


}
void MyDataGrid_PageIndexChanged(object source,DataGridPageChangedEventArgs e)
{
grid1.CurrentPageIndex=e.NewPageIndex;
grid1.DataBind();

lblCurrentIndex.Text = "当前页是:" + (grid1.CurrentPageIndex+1);
lblPageCount.Text = "共 " + grid1.PageCount+" 页";


}
void MyDataGrid_CurrentPage(object source,EventArgs e)
{
grid1.CurrentPageIndex=find_page.SelectedIndex;

lblCurrentIndex.Text = "当前页是:" + (grid1.CurrentPageIndex+1);
lblPageCount.Text = "共 " + grid1.PageCount+" 页";
DataBind();

}

private void cmdSelectAllClick(object sender,EventArgs e)
{
System.Web.UI.WebControls.CheckBox chkExpt;
//如果全选
if (cmdSelectAll.Text=="全选"
{
foreach(DataGridItem odi in grid1.Items)
{
chkExpt=(CheckBox)odi.FindControl("chkExpt";
chkExpt.Checked=true;
}
cmdSelectAll.Text="全消";
}
else
{
foreach(DataGridItem odi in grid1.Items)
{
chkExpt=(CheckBox)odi.FindControl("chkExpt";
chkExpt.Checked=false;
}
cmdSelectAll.Text="全选";

}

}

private void Del_Click(object sender, System.EventArgs e)
{
foreach (DataGridItem item in grid1.Items)
{
CheckBox c=(CheckBox)item.FindControl("chkExpt";
Response.Write(c.Checked); 就是这里chkExpt选中与不选中一个样,输出的都是chkExpt的初始值
if (c.Checked)
Response.Write(c.Text + "";
}
}

</script>
<body background="../../images/beijing.jpg">
<form runat=server>
<div align="center">
<table bgcolor="" cellpadding="6" width="100%">
<tr>
<td nowrap valign="middle" align="right">
<asp:Label id="lblCurrentIndex" ForeColor="red"
runat="server"/><br>
转到第:<aspropdownlist id="find_page" autopostback="True" onselectedindexchanged="MyDataGrid_CurrentPage" runat="server" />页&nbsp;
<asp:Label id="lblPageCount" ForeColor="red"
runat="server"/><br>

</td>
</tr>
</table>
    </div>
    
<%-- NumericPages NextPrev--%>
<table id="table1" border="0" align="center" width="100%">
<tr>
<td>
<aspataGrid id="grid1" runat=server AutoGenerateColumns="false" align="center" width="100%"
AllowPaging="True" PageSize="20" PagerStyle-Mode="NextPrev" DataKeyField="id"
PagerStyle-NextPageText="下一页" PagerStyle-PrevPageText="上一页"
OnPageIndexChanged="MyDataGrid_PageIndexChanged">
<HeaderStyle BackColor="#FAFAFA">
</HeaderStyle>

<PagerStyle Mode="NextPrev" NextPageText="下一页" PrevPageText="上一页" HorizontalAlign="Right" ForeColor="Black"
Position= "TopAndBottom" BackColor="#C6C3C6"></PagerStyle>

<ItemStyle BackColor="#E1F1FB">
</ItemStyle>
<AlternatingItemStyle BackColor="#Efefef">
</AlternatingItemStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="chkExpt" EnableViewState="false" Checked="True" Runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="编号">
<ItemTemplate>
<asp:Label ID="bianhao" Runat="server" text=<%#DataBinder.Eval(Container.DataItem,"id"%>></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="" DataField="ID" Visible="False" ReadOnly="true" />

<asp:BoundColumn HeaderText="用户编号" DataField="UseID" ReadOnly="true" />

<asp:BoundColumn HeaderText="登陆IP" DataField="IP" ReadOnly="true" />

<asp:BoundColumn HeaderText="登陆时间" DataField="LoginTime" ReadOnly="true" />

<asp:BoundColumn HeaderText="退出时间" DataField="LoginOffTime" ReadOnly="true" />

<asp:BoundColumn HeaderText="是否登陆成功" DataField="Valid" ReadOnly="true" />
</Columns>


</aspataGrid>
</td>
</tr>
<tr bgcolor="#99ccff">
<td>
<asp:Button id="cmdSelectAll" onClick="cmdSelectAllClick" EnableViewState="false" Runat="server" Text="全选"></asp:Button>
</td>
</tr>

<tr bgcolor="#99ccff">
<td>
<asp:button ID="cmdFindSelected" onClick="Del_Click" Text="DEL" Runat="server"></asp:button>
</td>
</tr>
<tr bgcolor="#99ccff">
<td>
</td>
</tr>
<tr bgcolor="#99ccff">
<td>
</td>
</tr>
<tr bgcolor="#99ccff">
<td>
<asp:Label ID="msg" Runat="server" text="aa"></asp:Label>
</td>
</tr>
</table>


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


hotinfo

老刀把子

职务:普通成员
等级:1
金币:0.0
发贴:207
#32004/11/16 13:18:58
把Page_Load里的DataGrid初始化放进if (!IsPostBack)里面去