Hi

I have a web form with a label on it. The content of that label is updated in response to some javascript on the client webpage.
Upon completion of some data entry, a button is clicked which adds that labels data (and others) to a gridview.

Basically this all works except that I can not retrieve thae updated value of the label. The grid addrow routine always finds the labels default value and writes that instead.
I assume this is a postback related issue so I created a hidden field and wrote the value to that also and then pull that for the grid write. Same problem...

How can I write a value to a label with javascript and then pull that value (server side) in asp.net (VB) for writing into the grid?


This code writes to the label and hidden field-

function jsCalcFrames() {
        var DurFrames = calcDurFrames();
        var sVal1 = document.getElementById('<%= lbl_Segment_SoM.ClientID %>').textContent;//.innerText; supposedly ie only
        var SoMframes = ConvertToFrames(sVal1);
        var EoMframes = ConvertFromFrames(DurFrames + SoMframes - 1);
        //  lbl_Segment_EoM.Text = EoMframes; //remove 1 as eom is before som
        document.getElementById('<%= lbl_Segment_EoM.ClientID %>').textContent = EoMframes;
        document.getElementById('<%= hidden_Segment_EoM.ClientID %>').textContent = EoMframes;
        //write to hidden field for persistence
        if (DurFrames  > 0) {
            Me.btnAppendSegment.Enabled = True;
        }
    }

This partial code is triggered by the forms append button -

Protected Sub btnAppendSegment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAppendSegment.Click
        appendSegmentArray(segIndex)  
end sub

    Private Sub appendSegmentArray(ByVal _index As Integer)
        If xmlCheck_SegTitle <> 14 Then
            MsgBox("You must give a title to this segment!", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Segment data error!")
            Exit Sub
        End If

        Dim sValDur As String = Me.txt_Segment_Dur_H.Text + ":" + _
                                Me.txt_Segment_Dur_M.Text + ":" + _
                                Me.txt_Segment_Dur_S.Text + ":" + _
                                Me.txt_Segment_Dur_F.Text

        dt = TryCast(ViewState("SegmentData"), DataTable)

        dt.Rows.Add(CInt(Me.lbl_Segment_Number.Text), Me.txt_Segment_Title.Text, Me.lbl_Segment_SoM.Text, Me.hidden_Segment_EoM.Value, sValDur)
        dt.Rows.Add(CInt(Me.lbl_Segment_Number.Text), Me.txt_Segment_Title.Text, Me.lbl_Segment_SoM.Text, Me.lbl_Segment_EoM.Text, sValDur) '+index
        '  ExecuteInsert(CInt(Me.lbl_Segment_Number.Text), Me.txt_Segment_Title.Text, Me.lbl_Segment_SoM.Text, Me.lbl_Segment_EoM.Text, sValDur)
        ViewState.Add("SegmentData", dt)
        dgvSegments.DataSource = dt


        dgvSegments.DataBind() 'updates

'other code snipped off

This line writes the gride -
dt.Rows.Add(CInt(Me.lbl_Segment_Number.Text), Me.txt_Segment_Title.Text, Me.lbl_Segment_SoM.Text, Me.hidden_Segment_EoM.Value, sValDur)

Me.hidden_Segment_EoM.Value has the equiv label of Me.lbl_Segment_EoM.Text

In this function, Me.lbl_Segment_EoM.Text always has the forms initial value despite that value having changed in the client form.
I need to write the reality client value.

Any clues anyone?

Recommended Answers

All 4 Replies

I had this problem once and although my solution probably isn't the correct one, I had a hidden field with a textbox (not a label) to hold the value. While label text updated via javascript doesn't seem to be available server side, the contents of a textbox is.

Hope that helps,
Steve

Thanks Steve

That was the solution I hit upon also but I cannot retreive the correct value from the hidden field either.
I can write and read it in Java but the vb/asp returns a null value.
Its like the vb/asp is reading the values for the initially created page rather than the updated one thats doing the postback.

Anyone else?

Malcom

In case it helps, here's my code for the control creation. maybe I'm missing something here?

<asp:label ID="lbl_Segment_EoM" runat="server" height="20px" 
               width="135px" font-size="Medium" style="text-align: center" 
               Borderstyle="Inset" Borderwidth="1px" Font-Names="BankGothic Md BT"                      
               ToolTip="The programme segments timecode 'end of message'.  This value will be automatically calculated based on the SoM value and the duration value that you enter.">00:00:00:00</asp:label>      
           <asp:HiddenField ID="hidden_Segment_EoM" runat="server" />

Thanks
Malcom

OK figured this one out.
I stupidly had the wrong setting for the hidden control. 'TextContent' instead of 'value'
Original code was-
document.getElementById('<%= hidden_Segment_EoM.ClientID %>').textContent = EoMframes;
Should have been -
document.getElementById('<%= hidden_Segment_EoM.ClientID %>').value = EoMframes;

All good until the next problem.
Thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.