开发者

FormView not updating with control events

开发者 https://www.devze.com 2022-12-16 05:34 出处:网络
Time for my daily ASP.NET question. One of my pages shows all of our customer information from a customer table.I want the user to choose whether to see all customer records, or select a specific rec

Time for my daily ASP.NET question.

One of my pages shows all of our customer information from a customer table. I want the user to choose whether to see all customer records, or select a specific record from a list. So, my webpage has two radio buttons (show all customers, show specific customer), a listbox (full of customer names), and a formview control. The problem I'm having is getting the formview to update when I change modes via radio buttons or listbox selection (see code below).

Can anyone provide me with some pointers on how to do what I'm trying to do?

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

protected void RadioButtonShowAll_CheckedChanged(object sender, EventArgs e)
{
}

protected void RadioButtonShowSelected_CheckedChanged(object sender, EventArgs e)
{
}

protected void DropDownListCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
    RadioButtonShowSelected.Checked = true;
    UpdatePage ();
}

protected void UpdatePage ()
{
    if (RadioButtonShowAll.Checked)
        SqlDataSource1.SelectCommand = "SELECT * FROM [Customer] ORDER BY [Company]";
    else
        SqlDataSource1.SelectCommand = "开发者_如何学CSELECT * FROM [Customer] WHERE ([CustomerID] = @CustomerID) ORDER BY [Company]";

    FormView1.DataBind();
}


First, you only have the SelectedIndexChanged event wired up... In that case, what happens when you change the drop down box is first, Page_Load() fires--which calls UpdatePage(). Then, the event fires, which calls UpdatePage() again. The second time probably doesn't do what you expect.

The fix is to only call UpdatePage() from Page_Load() the first time the page is loaded, but not from postbacks:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
        UpdatePage();
}


Your page has to be updated on Client side, for it to show the new data. You'll need to use Javascript or AJAX and have some variable that keeps track of the need to refresh your page, this way you can send a request to update the page to the server when the Formview needs updating.

0

精彩评论

暂无评论...
验证码 换一张
取 消