开发者

C# GridView dynamically built columns with textboxes ontextchanged

开发者 https://www.devze.com 2022-12-22 05:39 出处:网络
My page is a bulk order form that has many products and various size optio开发者_Go百科ns. I\'ve got a gridview that has a 3 static columns with labels.

My page is a bulk order form that has many products and various size optio开发者_Go百科ns.

I've got a gridview that has a 3 static columns with labels. There are then some dynamically built columns.

Each of the dynamically built columns have a textbox in them. The textbox is for quantity.

Trying to either update the server with the quantity entered each time a textbox is changed (possibly ontextchanged event) or loop though each of the rows column by column and gather all the items that have a quantity and process those items and their quantities all at once (via button onclick).

If I put the process that builds the GridView behind a if(!Page.IsPostBack) then the when a textchanged event fires the gridview only gets the static fields and the dynamic ones are gone.

If I remove the if(!Page.IsPostBack) the process to gather and build the page is too heavy on processing and takes too long to render the page again.

Some advice would be appreciated.

Thanks


I ended up building the columns dynamically in part by modifying and using the GridViewTemplate.cs I found here.

Since my textboxes were named based on their column name (which was based on the size options) I was able to loop through the size options and use FindControl to get the text box and its value.

protected void cmdSave_OnClick(object sender, EventArgs e)
{
    ArrayList itemsOrdered = new ArrayList();
    foreach (GridViewRow gvr in gvMainOrderForm.Rows)
    {
        Label lblItemId = (Label)(gvr.FindControl("lblItemId"));
        string itemId = lblItemId.Text;
        foreach (string availableOption in availableOptions)
        {
            TextBox tb = (TextBox)(gvr.FindControl("tb" + availableOption));
            if (tb != null && tb.Text != "")
            {
                ArrayList itemOrdered = new ArrayList();
                itemOrdered.Add(itemId);
                itemOrdered.Add(availableOption);
                itemOrdered.Add(tb.Text);
                itemsOrdered.Add(itemOrdered);
            }
        }
    }
}

If the value was not empty then I built a small array that had the product ID, size option and quantity.

Now I'll be able to use the itemsOrdered to modify my shopping cart.


how do you build the dynamic columns? are you handling events such rowdatabound, rowcreated, or are you rendering injected html with something like:

<asp:Gridview ...>
<Columns>
    <asp:TemplateField ...>    
        <ItemTemplate>   
           <%# GetDynamicHtml(Container.DataItem) %>
        </ItemTemplate>
    </asp:TemplateField
...

Is ajax an option?

0

精彩评论

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