Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# Dropdown box

Posted on 2010-08-17
11
Medium Priority
?
694 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
[X]
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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

704 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