简体   繁体   English

如何在JavaScript函数中使用/引用所选值(TextBox)到后面的C​​#代码中?

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

aspx.cs: 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: 我试图在cs后面的代码上使用:

COLOR_ALPHA_TEXTBOX1.SelectedValue 

But, I don't get that option in C#; 但是,我在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. 首先修复ASPX标记,第一个</asp:TableRow>应该是<asp:TableRow>否则标记将不匹配。

Second, TextBox doesn't have SelectedValue property, it has Text propetrty. 其次, TextBox没有SelectedValue属性,它具有Text属性。

And third - you cannot access inner nested control directly, you have to use FindControl to find it: 第三,您不能直接访问内部嵌套控件,而必须使用FindControl来找到它:

(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? 您正在使用jQuery还是AjaxControlToolkit?

This issue is usually caused by one of two things: 此问题通常是由以下两种原因之一引起的:

  • The .cs isn't inheriting from System.UI.Page .cs不是从System.UI.Page继承的
  • The .aspx doesn't have the correct value in the codebehind attribute: .aspx在codebehind属性中没有正确的值:

<%@ 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 : 但是,对于文本框, SelectedValue错误的属性,如果要使用其文本,则应使用Text

COLOR_ALPHA_TEXTBOX1.Text 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从asp中的javascript jquery中获取选定的值,以便在C#后面的代码中使用它 - how to get selected values from javascript jquery in asp to use it in the code behind of C# 从后面的C#代码向Javascript函数传递值 - Pass a value to a Javascript function from C# code behind 如何从c#代码后面调用javascript函数 - how to call a javascript function from c# code behind 使用 c# 显示 html 文本框,其中包含来自代码后面的值 - Dispay html textbox with value from code behind using c# 从代码后面添加JavaScript引用(C#) - Add JavaScript reference from code behind (C#) 我如何在不使用FindControl()方法的情况下,在后面的代码中使用位于gridView中的文本框的值 - How I can use the value of a textbox which is in a gridView in the code behind without using the FindControl() method 如何在C#代码中存储RadComboBox选择的值? - how can I store the RadComboBox selected value in C# code? 从JavaScript函数获取值到我的ASP.NET/C#代码隐藏 - Getting a value from a JavaScript function to my ASP.NET / C# Code-Behind 从后面的代码设置javascript变量的值(C#) - Setting value for javascript variable from code behind (C#) 将键/值对从 javascript 传递到 C# 代码后面 - passing key/value pairs from javascript to C# code behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM