Solved

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

Posted on 2011-02-21
2
415 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

615 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