Showing posts with label fields. Show all posts
Showing posts with label fields. Show all posts

Thursday, March 29, 2012

Poplulating Text Field with DropDown selection

Hi folks. Here's a simple question. Trying to populate some text fields based on a dropdown menu selection, but it doesn't work. When a selection is made in the dropdown menu, it seems to default to the selectedValue =2. Not sure why. Any advice would be much appreciated. Thanks! Here's the code:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { TextBox1.Text =""; TextBox2.Text =""; TextBox3.Text ="";if (DropDownList1.SelectedValue =="0") TextBox1.Text ="blah";if (DropDownList1.SelectedValue =="1") TextBox1.Text ="blahblah";if (DropDownList1.SelectedValue =="2") TextBox1.Text ="blahblahblah"; }

Are you dynamicalluy populating the dropdown list in your code behind? If you are, the Page_OnLoad event is running which is populating your dropdownlist on every postback. To make sure this doesnt happen, you should populate when teh page is not posted back. in other words...

if(!Page.IsPostback){// populate my dropdownlist}

If you are not dynamically populating the dropdownlist, please make sure your viewstate is turned on, that way the server knows that index to preserve when the page is loaded again.

EnableViewState="true"

Hope this helps.


If you are adding the items to the dropdown through code in Page_Load then check if they are inside If (!Ispostback) block.


hi..

i think u can have a break point in the page load or start of selected index changed event..

and find the value..

if not..

check in the design view that item 2 isSelected=true..

correct me if i'm wrong..


Thanks to all for the suggestions. The dropDownList was statically populated from the control properties in the ASPX page. I tinkered with this for a long while, but could NOT get it to work. So, I changed approach and populated the list by making an array in the code, and now it works as designed. Thanks again. Here is the code, in case anybody is interested.

protected void Page_Load(object sender, EventArgs e) {if (!IsPostBack) {// 2 dimensional array for dropDown liststring[,] forms = { {"blah","0"}, {"blah blah","1"}, {"blah blah blah","2"} };// populate listfor (int i = 0; i < forms.GetLength(0); i++) {//add both text and value DropDownList1.Items.Add(new ListItem(forms[i, 0], forms[i, 1])); } } }protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) {if (DropDownList1.SelectedIndex != -1) { TextBox1.Text = DropDownList1.SelectedItem.Text; } }

FYI... regarding the orginal post, the reason the textBox wasn't populated correctly was a simple syntax error. Used cascading IF statement without ELSE IF or switch statement.

Saturday, March 24, 2012

Populate fields

Hi:

I am looking for lookup table ability so the user will not have re-enter student information data. I have a table called for example "StudentInfo" - lookup table and another table called StudentActivity. My environment is VS.net/SQL/C#

I would like to have a form that would allow user to to select specific student (eg. from the drop down or listbox from StudentInfo table) and populate StudentActivity table based fields with the values from StudentInfo table, based on user selection.

ENTRY FORM (bound to StudentActivityTable)

Select Student: drop down menu (from StudentInfo)

Student name (populated from StudentInfo)

Student age (populated from StudentInfo)

Student address (populated from StudentInfo_

Location (user input)

Date: (user input) etc.

I would appreciate your assistance.

Danka

You need to get to grips with the basics of data access. http://quickstarts.asp.net/QuickStartv20/aspnet/doc/data/default.aspx

Here is a tutorial that is doing something like what you describe. It pairs a DropDownList with a DetailsView: http://msdn2.microsoft.com/en-us/library/aa581790.aspx

The DetailsView displays individual records, and can be used to edit or delete them. If the DetailsView control doesn't give you what you want, you could try the FormView control. I think the FormView offers greater flexibility in formatting the appearance.

Hope this helps


Yes, this is a good introduction for me. Thanks.


Hi:

I implemented it as a find compnay feature that provides user with a view of detailed company information based on the company selection in the drop down list.

Regarding my previous question related to drop down selection I tried the following code on someones advise. When user selects a value from the pull down menu this code updates templated field which is bound to database field. In the insert template I made this control not visible. I probably could implement the same on the edit template. It should be a better way than this. I am all new to it so I would appreaciate your thoughts.

} protectedvoid DropDownList1_SelectedIndexChanged(object sender,EventArgs e) { TextBox txtName = (TextBox)DetailsView1.FindControl("txtInsertLegalCompanyName"); DropDownList ddlName = (DropDownList)DetailsView1.FindControl("DropDownList1"); txtName.Text = ddlName.SelectedValue;

Now, I also have radio buttons list with two values Yes and No (true and false). I need some piece of code that would allow to show the selected value on the edit template (yes or no). Check box list allow to select both values True and False so I probably need some piece of code here so if one is selected the other one is deselected. I would also like to dispay Yes instead true, in vb I would use IIF... statment...

Your help would be appreaciated.

populate FormView fields from a DropDownList

Hi,
I'm trying to build an online form using asp.net and vb.net for the
code behind in Visual Studio 2005.
I want to fill 3 fields in a FormView that's connected to one table
(table1) with information from another table (table2) by choosing the
key field from table2 in a DropDownList.
Table2 has 3 fields with info that doesn't change (name, phone# and
email for about 5 customer service reps). I want each rep to choose
their name in the DropDownList to populate those fields in the
FormView.
I'm connecting to the sql db with an ObjectDataSource.
I'm thinking I just need to put some VB code into the
SelectedIndexChanged event of the ddl, something like:
*something goes here* = DropDownList1.SelectedValue.ToString()
But I'm drawing a blank on what would go to the left of the equal
sign.
Thanks in advance for the help.myFormView.FindControl("myControlId") will find the control if it is in the
template the formview is going to present. Otherwise you may need to do
something like myFormView.EditItemTemplate.FindControl("myControlId").
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"mkidd" <michaelk@.volkcorp.com> wrote in message
news:19842b1a-bab5-4594-aaef-c1cf99efd809@.u10g2000prn.googlegroups.com...
> Hi,
> I'm trying to build an online form using asp.net and vb.net for the
> code behind in Visual Studio 2005.
> I want to fill 3 fields in a FormView that's connected to one table
> (table1) with information from another table (table2) by choosing the
> key field from table2 in a DropDownList.
> Table2 has 3 fields with info that doesn't change (name, phone# and
> email for about 5 customer service reps). I want each rep to choose
> their name in the DropDownList to populate those fields in the
> FormView.
> I'm connecting to the sql db with an ObjectDataSource.
> I'm thinking I just need to put some VB code into the
> SelectedIndexChanged event of the ddl, something like:
> *something goes here* = DropDownList1.SelectedValue.ToString()
> But I'm drawing a blank on what would go to the left of the equal
> sign.
> Thanks in advance for the help.
Thanks, but can you help me with what to do to write the data from the
DropDownList table to the FormView after the FindControl event? I'm
really new at this.
On Jan 14, 9:15 am, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@.mMvVpPsS.org> wrote:
> myFormView.FindControl("myControlId") will find the control if it is in th
e
> template the formview is going to present. Otherwise you may need to do
> something like myFormView.EditItemTemplate.FindControl("myControlId").
> --
> Eliyahu Goldin,
> Software Developer
> Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
> "mkidd" <micha...@.volkcorp.com> wrote in message
> news:19842b1a-bab5-4594-aaef-c1cf99efd809@.u10g2000prn.googlegroups.com...
>
>
>
>
>
>
>
>
>

Populate form fields

How do you populate individual form fields from a database? I have used datasets in the past but this time I need to do individual form fields.

Below is the code I tried and I know that reader only read 1 parameter and not the way I have it setup now to do multiple parameters.

thanks

1Dim sConnStrAs String = ConfigurationManager.ConnectionStrings("accmon").ConnectionString23'************************45Dim MySqlAs String ="SELECT * FROM privacyscan WHERE privacyscanDelete = 0 and url ='" & privacyAddress & "'"67 Dim reader As OleDb.OleDbDataReader89 Dim MyConn As New OleDbConnection(sConnStr)10 Dim Cmd As New OleDbCommand(MySql, MyConn)1112 MyConn.Open()13 reader = Cmd.ExecuteReader()1415 While reader.Read()16 txtOA.Text = reader("oa")17'txtDate.Text = reader("date")18 'txtURL.Text = reader("url")19 'txtReport.Text = reader("report")20 'txtScan11.Text = reader("chkpt_scan_11")21 'txtPass11.Text = reader("chkpt_pass_11")22 'txtScan14.Text = reader("chkpt_scan_14")23 'txtPass14.Text = reader("chkpt_pass_14")24 'txtScan15.Text = reader("chkpt_scan_15")25 'txtPass15.Text = reader("chkpt_pass_15")26 'txtScan22.Text = reader("chkpt_scan_22")27 'txtPass22.Text = reader("chkpt_pass_22")28 'txtScan31.Text = reader("chkpt_scan_31")29 'txtPass31.Text = reader("chkpt_pass_31")30 'txtScan32.Text = reader("chkpt_scan_32")31 'txtPass32.Text = reader("chkpt_pass_32")32 'txtScan41.Text = reader("chkpt_scan_41")33 'txtPass41.Text = reader("chkpt_pass_41")34 'txtScan41a.Text = reader("chkpt_scan_41a")35 'txtPass41a.Text = reader("chkpt_pass_41a")36 'txtScan41b.Text = reader("chkpt_scan_41b")37 'txtPass41b.Text = reader("chkpt_pass_41b")38 'txtScan42.Text = reader("chkpt_scan_42")39 'txtPass42.Text = reader("chkpt_pass_42")40 'txtScan43.Text = reader("chkpt_scan_43")41 'txtPass43.Text = reader("chkpt_pass_43")42 'txtScan44.Text = reader("chkpt_scan_44")43 'txtPass44.Text = reader("chkpt_pass_44")44 'txtScan71.Text = reader("chkpt_scan_71")45 'txtPass71.Text = reader("chkpt_pass_71")46End While47 reader.Close()48 MyConn.Close()

Well, if you uncomment the lines below then your code should be working fine. However, if your query returns more than one row, your controls will only show the last row returned (since you have a while loop).

Another thing you might want to check is the layout of your table. It doesn't seem very normalized ;-)


What is the problem with the current code? However everytime you need to make sure you are returning only one row.