简体   繁体   English

asp.net中的下拉菜单控件

[英]dropdown controls in asp.net

In my webpage I have two dropdown controls for namely ddlmonth and ddldays ( for month & days) I write a method for current month in ddlmonth and current day in ddldays and i call that method in page load event it working fien but when i select (in ddlmonth ) different month and different day and insert into database the values inserted in database is current month and current day, actually i select different month and day 在我的网页中,我有两个下拉控件,分别是ddlmonth和ddldays(用于月份和天数),我在ddlmonth中编写了当前月份的方法,在ddldays中编写了当前日期的方法,我在页面加载事件中调用该方法,但当我选择(在ddlmonth)不同的月份和不同的日期,并插入数据库中插入的值是当前月份和当前日期,实际上我选择了不同的月份和日期

This is my code 这是我的代码

This is the method for current date display in ddlmonth dropdwon control and ddlday 这是在ddlmonth dropdwon控件和ddlday中显示当前日期的方法

public void getMonth()
{
    ddlmonth.SelectedIndex = DateTime.Now.Month -1;
    ddldate.SelectedIndex = DateTime.Now.Day - 1;
}

I call this in page load 我称其为页面加载

protected void Page_Load(object sender, EventArgs e)
{
    getMonth();
}

If I call the getMonth method in postback 如果我在回发中调用getMonth方法

protected void Page_Load(object sender, EventArgs e)
{
    if(!ispostback)
    {
        getMonth();
    }
}

it not displaying the current month and date 它不显示当前的月份和日期

without postback it is working 没有回发就可以了

When I select different month and day and insert into database it is taking current month and day 当我选择其他月份和日期并将其插入数据库时​​,它将占用当前月份和日期

Please help me 请帮我

您可以尝试使用Request.Form [“ ddlmonth”]和Request.Form [“ ddlday”]来检索值。

Sorry if I misunderstood, but to "call the getMonth method in postback", you need check for ispostback = true, not false. 抱歉,如果我误解了,但是要“在回发中调用getMonth方法”,则需要检查ispostback = true,而不是false。

protected void Page_Load(object sender, EventArgs e)
{
    if(isPostback)
    {
        getMonth();
    }
}

I think u should use 我认为你应该使用

protected void Page_Load(object sender, EventArgs e)
{
    if(!ispostback)
    {
        getMonth();
    }
}

The page is loaded and dropdowns showing Current date and month. 该页面已加载,并且下拉列表显示了当前日期和月份。 After that you choose different date and month and click some button to save that in database. 之后,您选择其他日期和月份,然后单击一些按钮以将其保存在数据库中。 In the button event you write the insertion code and if required then there again call getMonth() method to load again the current date and month in the dropdowns. 在按钮事件中,您编写插入代码,如果需要,则再次调用getMonth()方法以再次加载下拉列表中的当前日期和月份。 The date u chose in the dropdowns will be saved in DB. 您在下拉菜单中选择的日期将保存在DB中。 If required you call getMonth() method to load current date again as below. 如果需要,可以调用getMonth()方法再次加载当前日期,如下所示。

  protected void btnSave_Click(object sender, EventArgs e)
        {
             //-------- insertion code ----------------//
              getMonth();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM