Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 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