标签值传递给javascript

时间:2016-08-02 05:09:15

标签: javascript asp.net vb.net aspxgridview

我有一个标签,我通过后端编码存储它的价值。我想获得label的值并传递给Javascript。请帮我。一切正常,但是当我添加更新面板时,它们现在显示0值。我需要放置更新面板,以便页面不会刷新每个单击的gridview行。

这是我到目前为止所拥有的。

 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
 <ContentTemplate>
 <asp:Label ID="my_graph" runat="server"></asp:Label>
 </ContentTemplate>
 </asp:UpdatePanel>

<asp:UpdatePanel ID="UpdatePanel4" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" class = "grd-view table table-hover" OnRowDataBound="GridView1_RowDataBound"  OnSelectedIndexChanged="Gridview1_OnSelectedIndexChanged"  runat="server">
</asp:GridView>                                                
</ContentTemplate>
</asp:UpdatePanel>

<div id="chartContainer" style="height: 150px; width: 100%;"></div>

的JavaScript

 var pie = 0;
       function changepie(val) {
       pie = val;

       }
       function pageLoad() {
            alert(pie)
           }

后端

Protected Sub Gridview1_OnSelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged

        Dim percent As String = GridView1.SelectedRow.Cells(6).Text
        my_graph.Text = percent

        ClientScript.RegisterClientScriptBlock(Me.[GetType](), "Script", "changepie(" + my_graph.Text + ");", True)

    End Sub

1 个答案:

答案 0 :(得分:0)

那是因为您将Gridview放在不同的Update面板中。尝试放Label&amp;同一个Updatepanel中的Gridview

<强>更新

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Label ID="my_graph" runat="server"></asp:Label>
        <asp:GridView ID="GridView1" class = "grd-view table table-hover"             
        OnRowDataBound="GridView1_RowDataBound"  
        OnSelectedIndexChanged="Gridview1_OnSelectedIndexChanged"  runat="server">
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

或者将触发器添加到第一个UpdatePanel,如下所示

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <ContentTemplate> 
        <asp:Label ID="my_graph" runat="server"></asp:Label>
    </ContentTemplate>
    <Triggers>
         <asp:AsyncPostBackTrigger ControlID="Gridview_1" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>