Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
This is how it works: when the user clicks the TextBox, a palette of colors comes up, and then the user selects the color on a client-side with the JavaScript Function. So, what I need is that value of that color selected. Either get the JS as a text into the (asp:TextBox) to reference the value in C# or get the TextBox value selected directly into C#, if that makes sense.

This is my JavaScript Code:
JavaScript
<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>

This is my aspx Code:
ASP.NET
<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>

This is my C# Code:
C#
private void SaveNumberOfColors() 
{
SqlCommand cmd = null;
string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    string queryString = @"INSERT INTO NUMBER_COLORS " +
    "VALUES(" +
    "@SiteID, @AlphaFirst )";

        using (SqlConnection connection =
                   new SqlConnection(conn))
        {
            SqlCommand command =
                new SqlCommand(queryString, connection);
            connection.Open();
            cmd = new SqlCommand(queryString);
            cmd.Connection = connection;
            cmd.Parameters.Add(new SqlParameter("@SiteID", 
            System.Data.SqlDbType.NVarChar, //SqlDbType value
            20, //The width of the parameter
            "Site_ID")); //The name of the column source
            //Fill the parameter with the value retrieved
            //from the text field
            cmd.Parameters["@SiteID"].Value = foo.Site_ID;

            cmd.Parameters.Add(new SqlParameter("@AlphaFirst", 
            System.Data.SqlDbType.NVarChar, //SqlDbType value
            20, //The width of the parameter
            "AlphaFirst")); //The name of the column source
            //Fill the parameter with the value retrieved
            //from the text field
            cmd.Parameters["@AlphaFirst"].Value = ?????;          

            cmd.ExecuteReader();
        }
    }
Posted
Updated 22-May-13 12:05pm
v4
Comments
Prasad Khandekar 22-May-13 16:59pm    
Once the form gets posted you will have the value on server side. Where is the problem?
Castellon 22-May-13 17:09pm    
I want to use the SelectedValue of the JS, to insert it into SQL db using the ExecuteReader() but I'm not sure how to retrieve that value and add it as a parameter in my C# code. thanks for the help!
Prasad Khandekar 22-May-13 17:17pm    
Your code behind can directly access the selected value via COLOR_ALPHA_TEXTBOX2.Text property. However it will be available only when the form gets posted.

Regards,
Castellon 22-May-13 17:22pm    
I tried that by setting the AutoPostBack="True" But, I get the following: "Object reference not set to an instance of an object"
Castellon 22-May-13 17:18pm    
How can I apply it in my code?

Assign the selected color value to an asp:hidden field in JS code and use that hdnField.Value in server side.
 
Share this answer
 
Comments
Castellon 22-May-13 17:50pm    
How can I apply what you suggested in my code. Thanks for the help!
private void SaveNumberOfColors() 
{
SqlCommand cmd = null;
string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
 
    string queryString = @"INSERT INTO NUMBER_COLORS " +
    "VALUES(" +
    "@SiteID, @AlphaFirst )";
 
        using (SqlConnection connection =
                   new SqlConnection(conn))
        {
            SqlCommand command =
                new SqlCommand(queryString, connection);
            connection.Open();
            cmd = new SqlCommand(queryString);
            cmd.Connection = connection;
            cmd.Parameters.Add(new SqlParameter("@SiteID", 
            System.Data.SqlDbType.NVarChar, //SqlDbType value
            20, //The width of the parameter
            "Site_ID")); //The name of the column source
            //Fill the parameter with the value retrieved
            //from the text field
            cmd.Parameters["@SiteID"].Value = foo.Site_ID;
 
            cmd.Parameters.Add(new SqlParameter("@AlphaFirst", 
            System.Data.SqlDbType.NVarChar, //SqlDbType value
            20, //The width of the parameter
            "AlphaFirst")); //The name of the column source
            //Fill the parameter with the value retrieved
            //from the text field
            cmd.Parameters["@AlphaFirst"].Value = COLOR_ALPHA_TEXTBOX1.Text;         
 
            cmd.ExecuteReader();
        }
    }
 
Share this answer
 
Assign the selected value to an asp:hidden field.
 
Share this answer
 
Assign the selected value to an asp. I think it will be enough
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900