首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

POSTBACK的困惑解决思路

2012-01-08 
POSTBACK的困惑我想取DropDownList中的SelectedValue,可是一POSTBACK就什么也没了,大家有好的办法取吗,谢

POSTBACK的困惑
我想取DropDownList中的SelectedValue,可是一POSTBACK就什么也没了,大家有好的办法取吗,谢谢大家

<%@ 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">

<script language="javascript" type="text/javascript">
function save(index)
{
  var id = index.replace('DropDownList1','hidden');
  var obj = document.getElementById(index);
  document.getElementById(id).value = obj.options(obj.selectedIndex).value;
  alert(document.getElementById(id).value);
  //document.getElementById('hidden').value += obj.options(obj.selectedIndex).value;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
  <Columns>
  <asp:BoundField HeaderText="test" DataField="test">  
  </asp:BoundField>
  <asp:TemplateField HeaderText="反馈">
  <ItemTemplate>
  <asp:DropDownList ID="DropDownList1" runat="server">
  <asp:ListItem Text="111" Value="111"></asp:ListItem>
  <asp:ListItem Text="222" Value="222"></asp:ListItem>
  <asp:ListItem Text="333" Value="333"></asp:ListItem>
  </asp:DropDownList>
  <asp:HiddenField ID="hidden" runat="server"/>
  </ItemTemplate>
  </asp:TemplateField>  
  <asp:CommandField HeaderText="操作" ShowSelectButton="True" />
  </Columns>
  </asp:GridView>
  <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
  <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
  </div>
  </form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
  protected void Page_Load(object sender, EventArgs e)
  {
  BindGridView();
  }

  public DataSet GetData()
  {
  DataSet ds = new DataSet();
  DataTable dt = new DataTable();
  dt.Columns.Add("test");
  dt.Rows.Add(new string[] { "111" });
  dt.Rows.Add(new string[] { "222" });
  dt.Rows.Add(new string[] { "333" });
  dt.Rows.Add(new string[] { "444" });
  dt.Rows.Add(new string[] { "555" });


  ds.Tables.Add(dt);
  return ds;
  }

  public void BindGridView()
  {
  this.GridView1.DataSource = GetData().Tables[0];
  this.GridView1.DataBind();
  }

  protected void Button1_Click(object sender, EventArgs e)
  {
  string select1 = ((HiddenField)this.GridView1.Rows[0].FindControl("hidden")).Value;
  string select2 = ((HiddenField)this.GridView1.Rows[1].FindControl("hidden")).Value;
  string select3 = ((HiddenField)this.GridView1.Rows[2].FindControl("hidden")).Value;
  string select4 = ((HiddenField)this.GridView1.Rows[3].FindControl("hidden")).Value;
  string select5 = ((HiddenField)this.GridView1.Rows[4].FindControl("hidden")).Value;
  }

  protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  {
  GridViewRow row = this.GridView1.SelectedRow;
  string select = ((DropDownList)row.FindControl("DropDownList1")).SelectedValue;
  //string value = ((HiddenField)row.FindControl("hidden")).Value;
  }
}

[解决办法]
在Page_Load事件中,把BindGridView();放在 if(!this.IsPostBack)语句中就可以了

热点排行