简体   繁体   中英

How can I use/reference the selected value (TextBox) from a JavaScript Function into C# Code behind?

aspx.cs:

<script type="text/javascript">
    $(function () {
        //ALPHA
        $('#COLOR_ALPHA_TEXTBOX1').colorPicker({ pickerDefault: "E1E1E1", colors: ["E1E1E1", "33CC00", "FFF000", "CC0000", "996600", "FF9900", "303030", "0066FF", "F9A7B0", "9A0EEA"], transparency: true });
        $('#COLOR_ALPHA_TEXTBOX2').colorPicker({ pickerDefault: "E1E1E1", colors: ["E1E1E1", "33CC00", "FFF000", "CC0000", "996600", "FF9900", "303030", "0066FF", "F9A7B0", "9A0EEA"], transparency: true });
    });
</script>

<asp:Table ID="Table" runat="server" style="border: medium solid #000000">
<asp:TableRow>
    <asp:TableCell ID="TC2BC" HorizontalAlign="left" VerticalAlign="top">
            <asp:TextBox ID="COLOR_ALPHA_TEXTBOX1" type="text" runat="server" Visible="False"></asp:TextBox>
    </asp:TableCell>
</asp:TableRow>
<asp:TableRow>
    <asp:TableCell ID="TC9BC" HorizontalAlign="left" VerticalAlign="top" >
    <asp:TextBox ID="COLOR_ALPHA_TEXTBOX2" type="text" runat="server" Visible="False"></asp:TextBox>
    </asp:TableCell>
</asp:TableRow>
</asp:Table>

I tried to use on the code behind cs:

COLOR_ALPHA_TEXTBOX1.SelectedValue 

But, I don't get that option in C#; What could be an alternative? Thanks so much for the help!

First of all fix ASPX markup, the first </asp:TableRow> should be <asp:TableRow> otherwise tags won't match.

Second, TextBox doesn't have SelectedValue property, it has Text propetrty.

And third - you cannot access inner nested control directly, you have to use FindControl to find it:

(TextBox)Table.Rows[0].Cells[0].FindControl("COLOR_ALPHA_TEXTBOX1").Text

why are you trying to get the selected value of a textbox? You should be using

  COLOR_ALPHA_TEXTBOX1.Text 

in the codebehind.

Also, don't use

Visible="false"

as this will cause the control to not be rendered. if you want to hide a control (not sure why you would here, though) use:

 style="display:none"

Are you using another library with this control? I don't see how you can be using a "color" picker without more code. Are you using jQuery or the AjaxControlToolkit?

This issue is usually caused by one of two things:

  • The .cs isn't inheriting from System.UI.Page
  • The .aspx doesn't have the correct value in the codebehind attribute:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="InheritSample.aspx.vb" Inherits="CodeBehindSamples.InheritSample"%>

however for a textbox SelectedValue is the wrong property, if you want its text you should be using Text :

COLOR_ALPHA_TEXTBOX1.Text 

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