Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-21
2
Medium Priority
?
419 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 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

879 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