Solved

.net c# add value to an asp:button, dynamically using java?

Posted on 2011-02-21
2
411 Views
Last Modified: 2012-05-11
hi all

im trying to get the value of a listbox to be inserted as the value of an asp:button, so i can send it onclick
but ive noticed that an asp:button does not have a value attribtue nor does an input button

how can i do this?
<script type='text/javascript'>
        function GetCategory() {
            var Val = document.getElementById('CurrentCategories').value;
            var button = document.getElementById('GetCategoryBut');
            button.value = val;
            button.click()
        }
                </script>
        <asp:ListBox runat="server" ClientIDMode="Static" ID="CurrentCategories" Rows="20" Width="185" ></asp:ListBox><br />
        <asp:Button runat="server" ClientIDMode="Static" ID="GetCategoryBut"   OnClick="LoadCat" />

################ code behind ###############
 protected void LoadCat(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        string EditID = btn.Text.ToString();
       
        Output.Text = EditID;
        UpdatePanel1.Update();
}

Open in new window

0
Comment
Question by:awilderbeast
2 Comments
 
LVL 10

Accepted Solution

by:
P1ST0LPETE earned 500 total points
ID: 34943129
To change the text of your button to the value of the selected list item, you need to change your JavaScript a bit.  Needs to be something like this:

function GetCategory()
{
    var select = document.getElementById('CurrentCategories');
    var button = document.getElementById('GetCategoryBut');
    button.value = select.options[select.selectedIndex].value;
}


Also, in regards to sending the value back to the server, using the button text is not the correct way to do it.  You already have the item selected on your list, why not just get the value of the selected list item on the server, and set output to that value.  See full example below:

Markup:
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function GetCategory()
        {
            var select = document.getElementById('CurrentCategories');
            var button = document.getElementById('GetCategoryBut');
            button.value = select.options[select.selectedIndex].value;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <div>
        <asp:ListBox ID="CurrentCategories" runat="server" onclick="GetCategory()">
            <asp:ListItem Value="Category1" Text="Category 1" />
            <asp:ListItem Value="Category2" Text="Category 2" />
        </asp:ListBox>
        <br />
        <asp:Button ID="GetCategoryBut" runat="server" Text="Click Me" OnClick="LoadCat" />
        <br />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:TextBox ID="Output" runat="server" />
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="GetCategoryBut" />
            </Triggers>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

Open in new window


Code Behind:
 
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void LoadCat(object sender, EventArgs e)
    {
        string category = CurrentCategories.SelectedValue;

        Output.Text = category;
        UpdatePanel1.Update();
    }
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:awilderbeast
ID: 34943240
thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now