大家帮忙看看,ViewState为什么不起作用?
我在这里给ViewState赋值:
protected void gviewJd_record_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
ViewState[ "Code_id "] = gviewJd_record.Rows[gviewJd_record.SelectedIndex].Cells[2].Text;
//判断目前是否处于编辑模式(EditIndex为1),则禁止选择换行
if (gviewJd_record.EditIndex != -1)
{
gviewJd_record.SelectedIndex = -1;
e.Cancel = true;
Literal txtMsg = new Literal();
txtMsg.Text = " <script> alert( '编辑模式下禁止选择换行! ') </script> ";
Page.Controls.Add(txtMsg);
}
}
在这里取值:
protected void gviewJd_record_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "New ")
{
string recordJd_name = ((DropDownList)gviewJd_record.FooterRow.FindControl( "NewJd_name ")).SelectedValue;
if (recordJd_name.Trim() == " ")
{
ErrorMsg.Text = "请输入检查类型! ";
return;
}
string recordCheck_date = ((TextBox)gviewJd_record.FooterRow.FindControl( "NewCheck_date ")).Text;
if (recordCheck_date.Trim() == " ")
{
ErrorMsg.Text = "请输入检查日期! ";
return;
}
string code_idd= " ";
if (ViewState[ "code_id "] != null)
{
code_idd = (string)ViewState[ "code_id "];
}
((TextBox)gviewJd_record.FooterRow.FindControl( "NewCode_id ")).Text = code_idd;
实际上code_idd是空的,为什么?
[解决办法]
SelectedIndexChanging?这事件没用过
在里面设置断点,看viewstate有没赋到值
[解决办法]
你先调试一下看看程序是否走到了,再判断viewstate的值是否存在
[解决办法]
你现在是对ViewState不信任还是对你的业务流程不信任
如果对ViewState不信任,那去建一个简单页面来测试,不要包含业务逻辑
如果对业务逻辑不信任,那去跟踪调试一下看业务走向
心里有底了,解决问题来会得心应手一点
说多了,见谅
看你发了几个帖子
[解决办法]
看看SelectedIndexChanged