Solved

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

Posted on 2011-02-21
2
410 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

13 Experts available now in Live!

Get 1:1 Help Now