Solved

C# Dropdown box

Posted on 2010-08-17
11
683 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
Comment Utility
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
Comment Utility
its not working what is ctl00$cphAdmin?
Without java script there is a way??
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 3

Expert Comment

by:revaluser
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
and also check for enableviewstate in the page directive which is by default set to false
0
 

Author Comment

by:lankapala
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now