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

x
?
Solved

C# Dropdown box

Posted on 2010-08-17
11
Medium Priority
?
695 Views
Last Modified: 2013-12-17
Hi, i wrote below coding ,
<form>

</ <asp:DropDownList ID="select_week" runat="server"
                                          OnSelectedIndexChanged="Selection_Change"  AutoPostBack="true"  >
                                      </asp:DropDownList>

</form>

public void Selection_Change(Object sender, EventArgs e)
    {

       //rent_date.Text=select_week.SelectedItem.Value;
     rent_date.Text = this.select_week.SelectedItem.Value;
     

   }

its not working. I need actullay do is , when dropdown box click and after select , i need to put that value to textbox.(without  using submit button)

i checked any click event with DropDownList but i can't find out.Can any one explain me how to use that.

After click on the DropDownList need to change text box value. please help me to solve above issue.thx
0
Comment
Question by:lankapala
  • 3
  • 2
  • 2
  • +4
11 Comments
 
LVL 4

Expert Comment

by:yesthatbob
ID: 33460348
To do this in code behind you need to set the AutoPostback property of the DropDownList to true.
This is not the most elegant solution, however, as it will post the page back to the server. A better solution would be to use client side scripting to do the same thing.
0
 
LVL 4

Expert Comment

by:yesthatbob
ID: 33460391
As a follow-up to my last post, the way to add JavaScript to the aspx DropDownList control is to register its onclick event when the page loads. This adds the client-side script when the user requests the page.
In the attached code, you see that you can use the ClientID property of both controls to get the ID given to the controls by the server when the page is served up.

protected void Page_Load(object sender, EventArgs e)
	{
		this.select_week.Attributes.Add("onclick", 
				String.Format("{0}.value = {1}.value;", this.rent_date.ClientID, this.select_week.ClientID));
	}

Open in new window

0
 
LVL 10

Expert Comment

by:Jini Jose
ID: 33460899
here is your code



html

<asp:DropDownList ID="DropDownList1" runat="server" onclick="return displ();">
    </asp:DropDownList>
    <asp:TextBox ID="valu" runat="server"></asp:TextBox>

javascript

<script language="javascript">
function displ()
{
  if(document.aspnetForm.ctl00$cphAdmin$DropDownList1.options[0].value == true) {
    return false
  }
  else {
  alert(document.aspnetForm.ctl00$cphAdmin$DropDownList1.options[document.aspnetForm.ctl00$cphAdmin$DropDownList1.selectedIndex].value);
document.aspnetForm.ctl00$cphAdmin$valu.value=document.aspnetForm.ctl00$cphAdmin$DropDownList1.options[document.aspnetForm.ctl00$cphAdmin$DropDownList1.selectedIndex].value;
  }
  return true;
}
//-->
</script>

Open in new window

0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 33461785
Please try this one:

<form>
<asp:DropDownList ID="select_week" runat="server"  OnSelectedIndexChanged="Selection_Change"  AutoPostBack="true"  >  </asp:DropDownList>
</form>

public void Selection_Change(Object sender, EventArgs e)
{
     rent_date.Text = select_week.SelectedValue.ToString();
}

Regards,
Ram
0
 
LVL 2

Author Comment

by:lankapala
ID: 33461866
its not working what is ctl00$cphAdmin?
Without java script there is a way??
0
 
LVL 3

Expert Comment

by:revaluser
ID: 33462763
I guess your are missing the if(!ispostback) in the Page_load event.please do the necessary modification
0
 
LVL 14

Expert Comment

by:Dhanasekaran Sengodan
ID: 33463018
Solution
========

1) Bind the Value for Dropdownlist in Page Load

protected void Page_Load(object sender, EventArgs e)
        {
            if(!isPostBack)
          {
                          select_week.DataSource = // Your Datatable
                          select_week.DataBind();
        }
}


2) call javascript method on  your control

<asp:DropDownList ID="select_week" runat="server" onChange="javascript:onassign();" />


3) Write a Javascript

<script language= "javascript" type ="text/javascript">
function onCloningCompleted()
     {
     if (document.all["ctl00_select_week"].value != '')
        {
          document.all["ctl00_rent_date"].value = document.all["ctl00_select_week"].value;
        }
     else
        {
          document.all["ctl00_rent_date"].value ='';
        }
     }
</script>


Good Luck
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33466053
Hi,

Did your page attribute includes the code as AutoEventWireup set as true.

Regards,
VSS

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

Open in new window

0
 
LVL 3

Expert Comment

by:revaluser
ID: 33466859
and also check for enableviewstate in the page directive which is by default set to false
0
 
LVL 2

Author Comment

by:lankapala
ID: 33470305
please see my coding below
<script language= "javascript" type ="text/javascript">
function onCloningCompleted()
     {
     if (document.all["select_week"].value != '')
        {
          document.all["ctl00_rent_date"].value = document.all["ctl00_select_week"].value;
        }
     else
        {
          document.all["ctl00_rent_date"].value ='';
        }
     }
</script>

 <form id="aspnetForm" runat="server" name="aspnetForm">
     <br />
    <asp:DropDownList ID="select_week" runat="server" onChange="javascript:onassign();"  >
    <asp:ListItem>Test1</asp:ListItem>
    <asp:ListItem>Test2</asp:ListItem>
    </asp:DropDownList>
    <asp:TextBox ID="rent_date" runat="server"></asp:TextBox>
    </form>
AutoEventWireup set as true is already done
its not working can any show me where iam doing mistake. What is ctl00? my dropdownlist name is select_week
at least can any one show me godo example for that.thx
0
 
LVL 2

Accepted Solution

by:
lankapala earned 0 total points
ID: 33470657
i found the answer
 <asp:DropDownList ID="ddlStatus" runat="server" AutoPostBack="false" OnChange="SetDateFields()">

<script type="text/javascript">
function SetDateFields()
{
var statusList = document.getElementById('<%=ddlStatus.ClientID %>');
var selectedStatus = statusList.options[statusList.selectedIndex].text;
  var xx = document.aspnetForm.rent_date.value;
document.aspnetForm.rent_date.value=selectedStatus;


//alert('selected status is : ' + selectedStatus);
}
</script>

in the page load

ddlStatus.Attributes.Add("onchange", "javascript:SetDateFields()");
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
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…

926 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