Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将值传递到下一页C ASP.NET_C#_.net_Asp.net - Fatal编程技术网

C# 将值传递到下一页C ASP.NET

C# 将值传递到下一页C ASP.NET,c#,.net,asp.net,C#,.net,Asp.net,在我的代码中,当我在下拉列表中选择值并按enter键后,将改为发送下拉列表的第一个值 我可以知道可能的原因吗 下面是我的代码: protected void Button1_Click(object sender, EventArgs e) { if (DayBox.Text != "" && MonthBox.Text != "" && YearBox.Text != "") { string date; int

在我的代码中,当我在下拉列表中选择值并按enter键后,将改为发送下拉列表的第一个值

我可以知道可能的原因吗

下面是我的代码:

protected void Button1_Click(object sender, EventArgs e)
{
    if (DayBox.Text != "" && MonthBox.Text != "" && YearBox.Text != "")
    {
        string date;
        int container = Convert.ToInt32(DayBox.Text);

        if (container < 10)
        {
            date = String.Format("0{0}/{1}/{2}", DayBox.Text, MonthBox.Text, YearBox.Text);
        }
        else
        {
            date = String.Format("{0}/{1}/{2}", DayBox.Text, MonthBox.Text, YearBox.Text);
        }


        Session["DATE"] = date;
        Session["IPADDRESS"] = IPDropDownList.Text;

        Response.Redirect("IPAddressDay.aspx");
    }
}
说明: 它不会读取我选择的值,而是取第一个值并发送到下一页

您需要:

Session["IPADDRESS"] = IPDropDownList.SelectedValue;

由于选择框有一个value属性,该属性获取选项的值,该值可能与显示的文本不同。

如果在页面加载事件中填写下拉列表,则需要使用IsPostBack。每次点击按钮时,页面加载事件都会在按钮单击事件之前触发。因此,在您的按钮1_单击时,您总是会在dropdownlist中获得第一个值。正确的代码应该是

if (!IsPostBack && DayBox.Text == "" && MonthBox.Text != "" && YearBox.Text != "")
{
    IPDropDownList.Items.Clear();

    string date = String.Format("{0}/{1}",MonthBox.Text, YearBox.Text);

    sqlStatment = String.Format("SELECT IPAddress FROM IPActivity WHERE AccessDate LIKE '%{0}' GROUP BY IPAddress;", date);

    MyDataSet = RetriveDataBase(connection, sqlStatment, tableName);

    foreach (DataRow dr in MyDataSet.Tables[tableName].Rows;)
    {
        IPDropDownList.Items.Add(dr[0].ToString());
    }   
}
试试这个:

Session["IPADDRESS"] = IPDropDownList.SelectedValue;
然后,要设置该值:

IPDropDownList.SelectedValue = Session["IPADDRESS"] != null ? Session["IPADDRESS"].ToString() : "" ;
如果你回帖,不要重置你的下拉列表

if(!IsPostBack)
{
   //Code to set dropdownlist values here
}

将值发送到哪里?请详细说明您在哪里填写下拉列表?在页面加载事件中?嗨,杰米,我试过了,但还是不起作用。点击按钮后,页面将重新加载,这会导致页面获取DDL的第一个值。您能否显示下拉控件的代码以及如何填充该控件?我已将其附加到问题Nevayshirazi THX以获取帮助,但我希望该值在用户更改日期时随时更改。如果我加上IsPostBack,这个值将无法更改。你能告诉我吗?你怎么更改日期?我的意思是,据我从您的代码中了解,它只是文本框中的一个字符串?我通过允许用户从另一个DDL选择日期来更改日期,然后插入sql可以从数据库中获取。您可以将OnSelectedIndexChanged事件分配给用户选择日期的DDL。使用此事件更新您的日期信息,而不是页面加载。希望这有帮助。你有一个我如何使用它的例子吗?因为我对Asp.netHi ChuckTHX的帮助很陌生,但我希望每当用户更改日期时,该值都会更改。如果我加上IsPostBack,这个值将无法更改。你能给我一些建议吗如果不在每次加载页面时使用IsPostBack,您可以重新绑定下拉列表并清除用户选择的值。您只想在页面最初加载时绑定列表。Chuck向您展示了您需要的内容—将代码绑定到if语句中的下拉列表。
if(!IsPostBack)
{
   //Code to set dropdownlist values here
}