求助关于gridview中使用dropdownlist并启用分页后产生的问题
在gridview的模板列中使用dropdownlist绑定数据库中的数据后,并启用了分页功能后,第1页正常,dropdownlist中有显示数据;但转到第2页后dropdownlist就空了,再转到第1页(不是后退)也没有数据显示了,要再重新LOAD这个页面后,第1页的dropdownlist才会有数据。请问这个该如何解决,谢谢!
部分代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetReceiver();
}
}
private void GetReceiver()
{
string sql = "SELECT OutBoxDetail.OutBoxDetailId, OutBoxDetail.OutBoxId, OutBoxDetail.ReceiverId, UserInfo.UserId, UserInfo.LoginName, UserInfo.UserName, UserInfo.Gender, UserInfo.Mobile, UserInfo.Remark FROM OutBoxDetail INNER JOIN UserInfo ON OutBoxDetail.ReceiverId = UserInfo.UserId ";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[ "ConnectionString "].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "OutBox ");
for (int i = 0; i < gvOutBox.Rows.Count; i++)
{
GridViewRow row = gvOutBox.Rows[i];
int OutBoxId = int.Parse(row.Cells[0].Text);
DropDownList ddrReceiver = (DropDownList)row.FindControl ( "ddrReceiver ");
DataView dv = ds.Tables[ "OutBox "].DefaultView;
dv.RowFilter = "OutBoxId = " + OutBoxId.ToString();
ddrReceiver.DataSource = dv;
ddrReceiver.DataTextField = "UserName ";
ddrReceiver.DataBind();
}
}
[解决办法]
DropDownList ddrReceiver = (DropDownList)row.FindControl ( "ddrReceiver ");
DataView dv = ds.Tables[ "OutBox "].DefaultView;
dv.RowFilter = "OutBoxId = " + OutBoxId.ToString();
ddrReceiver.DataSource = dv;
ddrReceiver.DataTextField = "UserName ";
ddrReceiver.DataBind();
以上代码放在rowDataBind事件中执行即可