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
Solved

C# Dropdown box

Posted on 2010-08-17
11
687 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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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