Solved

C# Dropdown box

Posted on 2010-08-17
11
686 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 

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
 

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
 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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 …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

832 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