简体   繁体   中英

Get gridview template field Label value on button click event which is calculated using javascript

I have a Gridview( gvMRVdetails ) with many template fields, one is,

<asp:TemplateField HeaderText="Issued Total Qty" ItemStyle-Width="8%" ControlStyle-Width="98%">
    <ItemTemplate>
        <asp:Label ID="lblIssuedTotalQty" runat="server" Text='<%#Bind("TotalIssuedQty") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

Intially i bind 0.00 to it as TotalIssuedQty

then I will calculate New value for "lblIssuedTotalQty" by javascript on another text box( txtIssuedQty ) key press event which is also in the grid view. I can assign its value by following

function CalculateTotalQty(obj) {
        var grid = document.getElementById('<%=gvMRVdetails.ClientID %>');
        var rwIndex = 1;
        var gvRowCount = grid.rows.length;
        if (grid != null) {
            for (rwIndex; rwIndex <= gvRowCount - 1; rwIndex++) {

                var txtIssuedQty = grid.rows[rwIndex].cells[4].firstChild;
                var lblIssuedTotalQty = grid.rows[rwIndex].cells[9].firstChild;
                lblIssuedTotalQty.innerText = parseFloat(txtIssuedQty.value)*2 
                }
            }

it will be visible for example if I enter 2.5 in txtIssuedQty , lblIssuedTotalQty will display 5.It works fine. But my problem is I want to get the dynamically calculated value in a ButtonClickevent

 protected void btnAllocate_Click(object sender, EventArgs e)
    {
            foreach (GridViewRow GVR in gvMRVdetails.Rows)
            {
                    Label a = GVR.FindControl("lblIssuedTotalQty") as Label;
                    string s = a.Text;
        }
    }

I can get only the initial value 0.00 in sI can't get 5 in s.If i use textbox instead of label it,s possible but using label how to get it.

The label text value will not retain it's value on postback when changed on the client side. You have two options:

1) Try to get the value from the form values posted, like this:

Label a = GVR.FindControl("lblIssuedTotalQty") as Label;
string s = Request[a.UniqueID] as string;

2) Use a hidden field on the client side to store your value and then retrieve it on postback. Set the value for the hidden field and then you can retrieve it from codebehind

 <ItemTemplate>
    <asp:Label ID="lblIssuedTotalQty" runat="server" Text='<%#Bind("TotalIssuedQty") %>'>/asp:Label>
    <asp:hiddenfield id="hdnIssuedTotalQty"  runat="server"/>
</ItemTemplate>

Sureshhh i think the issue is because when you click on the server control button the page controls gets loaded so the label 'lblIssuedTotalQty' is again getting binded to 0.00 so when you access it in the code behind you get the value as 0.00.

As you have mentioned if you are going to use a textbox which will not get binded can make the new js calculated value available in the code behind.

FIX: Add any control which will you not bind to get its value (it could be even test box like you tried) on each row in Grid View and set its visibility to false . In the js set the calculated value to this invisible control and fetch it in the backend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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