如何获取前一个页面的控件的值。

zhoulier 2008-07-08 11:10:10
母版页:控件(textbox1,dropdownlist1,button1)C#代码如下

内容页:default.aspx(无内容);
内容页:default2.aspx(控件Gridview1)C#代码如下

调试正常无语法错误
问题点:在default.aspx页面,dropdownlist1选择第二个item,点击button1,转入default2.aspx页面,内容页显示的是默认值第一个item的结果。实际上没有将需要的值传递过来。请教如何解决?
...全文
241 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xlong224 2008-07-08
  • 打赏
  • 举报
回复
TextBox mptextbox=(TextBox )PreviousPage .Master .FindControl("TextBox1");
LiloZhu 2008-07-08
  • 打赏
  • 举报
回复
TextBox mptextbox = (TextBox)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("TextBox1");

zhoulier 2008-07-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhoulier 的回复:]
引用 2 楼 zhangyu_xl 的回复:
用previous属性的FindControl()方法。

能具体点吗?我是初学者
[/Quote]
从default.aspx到default2.aspx正常了,但是default2.aspx本身出问题了,是不是应该在此之前先if一下呢?
错误如下
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
行 20: TextBox mptextbox=(TextBox )PreviousPage .Master .FindControl("TextBox1");
zhoulier 2008-07-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhangyu_xl 的回复:]
用previous属性的FindControl()方法。
[/Quote]
能具体点吗?我是初学者
zhangyu_xl 2008-07-08
  • 打赏
  • 举报
回复
用previous属性的FindControl()方法。
zhoulier 2008-07-08
  • 打赏
  • 举报
回复
母版页:控件(textbox1,dropdownlist1,button1)C#代码如下
public partial class MasterPage : System.Web.UI.MasterPage
{
public string mpsearchtype
{
get { return DropDownList1.SelectedItem.Value.ToString(); }
}

protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("default2.aspx");
}
}

内容页:default2.aspx(控件Gridview1)C#代码如下
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string searchtype = Master.mpsearchtype;
TextBox mptextbox = (TextBox)Master.FindControl("TextBox1");
DropDownList mpdropdownlist = (DropDownList)Master.FindControl("DropDownList1");
//string searchtype=mpdropdownlist.SelectedItem.Value .ToString();
string searchwhat = mptextbox.Text;
// 1. Create the connection string and command string
string connectionString =
"Data Source=kingdee;Initial Catalog=aspnet;Persist Security Info=True;User ID=;Password=";
string searchtable = "customer";
if (searchtype == "fcustomerid" || searchtype =="fcustomername")
{
searchtable = "customer";
}
string commandString =
"Select * from " + searchtable + " where " + searchtype + " like '%" + searchwhat + "%'";

// 2. Pass the strings to the SqlDataAdapter constructor
SqlDataAdapter dataAdapter =
new SqlDataAdapter(commandString, connectionString);

// 3. Create a DataSet
DataSet dataSet = new DataSet();

// 4. fill the dataset object
dataAdapter.Fill(dataSet, "customer");

// 5. Get the table from the dataset
DataTable dataTable = dataSet.Tables["customer"];

// 6. Bind to the Gridview
GridView1.DataSource = dataTable;
GridView1.DataBind();

}
}


zhoulier 2008-07-08
  • 打赏
  • 举报
回复
问题已解决:主要修改代码如下
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string searchtype, searchwhat;
//判断是否是跨页面传送,跨页传送就用PreviousPage传值。
if (PreviousPage != null)
{
TextBox mptextbox = (TextBox)PreviousPage.Master.FindControl("TextBox1");
DropDownList mpdropdownlist = (DropDownList)PreviousPage.Master.FindControl("DropDownList1");
searchtype = mpdropdownlist.SelectedItem.Value.ToString();
searchwhat = mptextbox.Text;
}
else
{
searchtype = Master.mpsearchtype;
searchwhat = Master .mpsearchwhat;
}

谢谢各位。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧