Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through dropdownlists at serve rside

Status
Not open for further replies.

sparky0904

IS-IT--Management
Jan 15, 2002
72
0
0
GB
Hi,

i have a page using a repeater that lists 2 Combo boxes for each record on the repeated. I need to retrieve the value of the combo boxes when the save button is clicked for each record and push back into database.

Idea was to name each control uniquely with the ID of the record and then retrieve the value at server side to then fire a routine to push the results into a database.

In the page the comobobox has an ID of KnowledgeScore, using the itembound event i have added the ID of the record to the end of the ID so it know looks like KnowledgeScore-1, knowledgeScore-42, etc. Checking the source code of the HTML page once loaded i can see that each combobox has a unique ID.

I have a save button and in the click event i try to loop through the controls on the page and pick out those that have a type of dropdownlist, however the ID is that i have entered at design time and also the value of each comobo box is set to the first record as if the data has been lost.

I have the dropdownlists set to runat="Server".

Is this anything to do with postback data??

Can i do what i want to do??

 
place the row id in a hidden field and parse this value out, rather than munging control ids. example
Code:
<asp:hiddenfield Id="RecordId" RunAt="server" Value='<%Bind("Id")%>'/>
<asp:dropdownlist Id="KnowledgeScore" RunAt="server" .../>
Code:
//called when the button is clicked
private void Save(object sender, EventArgs e)
{
  foreach(var item in Repeater.Items)
  {
    if(Item.ItemType != ItemType.DataItem) continue;
    var id = ((HiddenField)Item.FindControl("RecordId")).Value;
    var score = ((DropDownList)Item.FindControl("KnowledgeScore")).SelectedValue;

    //use id and score to update database.
  }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
If you want just the values from the ddls in the repeater, then use the itemdatabound event and .findcontrol() to find the ddl and get it's value.
 
Hi,

Thanks for help however, still cannot get to work.

Funnily enough if i display the ispostback, when it is false it fails, however when it is showing as true if i change values then the routine below works!!!

Means though i need to click save twice and when the page is redisplayed it dows not show the values as per the database, all the comobos are defaulted to the first entry!

My routine is now:

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

Page.Validate()

If Page.IsValid Then
'theControlArrayList = New ArrayList

Dim myRepeaterItem As RepeaterItem

For Each myRepeaterItem In Me.repSkillAudit.Items
Dim myKnowledgeScorecmb As DropDownList = myRepeaterItem.FindControl("cmbKnowledgeScore")
Dim theKnowledgeSelectedValue As Integer = myKnowledgeScorecmb.SelectedValue
Response.Write(theKnowledgeSelectedValue.ToString)
Next
End If

End Sub



 
Hi

sorry jbenson in reply to you, i thoguht the itembound could only be used at display when the repeater is fired, sa i want to collect the data afterwards would the itembound fire again?


 
sparky, how well do you understand the asp.net life cycle and webform page life cycle? this is key in getting webforms to do what you want.

btw: you should never need to call response.write(). there are much better ways to debug the system.

as for the 1st option being selected. yes, if you don't set the selected value it will default to either nothing or the first option.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Hi,

not very well i am afraid, just learning from books. However i will go and look em up, thanks for a nudge in right direction

Agree with you on response.write, just keep going back to my Commodore 64 basic programming days :)

 
if you need to log what's happening check out log4net.

here are some diagrams of the asp.net and webforms life cycles. unfortunately many people interchange the frameworks, or believe they are one in the same, when they are not. asp.net handles response/redirects webforms is an html engine.

knowing when different events fire and the order they fire will help you gain control of your application.

as a side note: if you are just getting into asp.net development i would highly recommend research the concepts/frameworks of MVC instead of webforms. it's a much simpler API and gives you alot more control over the html and work flow.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
This is weird, i have created some hidden fields to capture the ID and they carry the value over through postback, however the comobos do not, am i missing something here??

I have read through the lifecycle and understand better now, i can see the correlation between the cycle and the page events which helps and i have tried debug.writing out the values at each stage but still no luck!!

Any ideas any one?

 
i know VB.Net allows for implicit casting. what happens if the cast is invalid.

reviewing your code above you do not check the type of Item returned (header, footer, separator, item, alternate). the controls would only be present in item & alternate.

but you did state you can retrieve the value of the hidden field, so this may not be the issue.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Hi,

I am only checking the repeater items as they hold the comobo boxes. I did try checking all the comobo boxes on page until i realised i could narrow down to just the repeater items.

What is puzzling me is how it keeps the hidden field values only from the first page load, however any subsequent clicks of the save button which cause the page to reload keeps any changes to the combo fields. Maybe i am just tired and cannot see wood for trees!

The new code is here.


Protected Sub GetComboValues()
Dim myRepeaterItem As RepeaterItem

For Each myRepeaterItem In Me.repSkillAudit.Items
Dim myKnowledgeScorecmb As DropDownList = myRepeaterItem.FindControl("cmbKnowledgeScore")
Dim myUsageScorecmb As DropDownList = myRepeaterItem.FindControl("cmbUsageScore")
Dim myIDHiddenField As HiddenField = myRepeaterItem.FindControl("IDHiddenField")

Dim theKnowledgeSelectedValue As Integer = myKnowledgeScorecmb.SelectedValue
Dim theUsageSelectedValue As Integer = myUsageScorecmb.SelectedValue

Debug.WriteLine("ID = " + myIDHiddenField.Value.ToString + ". " + "Knowledge Score = " + theKnowledgeSelectedValue.ToString + ". " + "Usage Score = " + theUsageSelectedValue.ToString)

Next

End Sub

 
try
Code:
 Protected Sub GetComboValues()
        Dim myRepeaterItem As RepeaterItem

        For Each myRepeaterItem In Me.repSkillAudit.Items

            If myRepeaterItem.ItemType <> ItemType.DataItem Then Continue

            Dim myKnowledgeScorecmb As DropDownList = myRepeaterItem.FindControl("cmbKnowledgeScore")
            Dim myUsageScorecmb As DropDownList = myRepeaterItem.FindControl("cmbUsageScore")
            Dim myIDHiddenField As HiddenField = myRepeaterItem.FindControl("IDHiddenField")

            Dim theKnowledgeSelectedValue As Integer = myKnowledgeScorecmb.SelectedValue
            Dim theUsageSelectedValue As Integer = myUsageScorecmb.SelectedValue

            Debug.WriteLine("ID = {0}. Knowledge Score = {1}. Usage Score = {2}", myIDHiddenField.Value, theKnowledgeSelectedValue, theUsageSelectedValue)

        Next

    End Sub
this may also be a problem. it would be in C#
Code:
Dim theKnowledgeSelectedValue As Integer = myKnowledgeScorecmb.SelectedValue
try this instead
Code:
Dim theKnowledgeSelectedValue As Integer = int.Parse(myKnowledgeScorecmb.SelectedValue)

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
one other thing to check where/when do you populate the dropdown?

the problem my be hiding in additional code.


Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Hi,

Thanks for guidance much appreciated.

When i try to enter the itemtype.dataitem line i cannot intellisense does not let me chose it, i am using VS2010 asp.net, maybe this is where my problem is ??

I do not get itemtype only ListItemType however there is not a selection for dataitem

I populate the data here:

Protected Sub repSkillAudit_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repSkillAudit.ItemDataBound

Dim item As RepeaterItem = e.Item

If item.ItemType = ListItemType.Item Or item.ItemType = ListItemType.AlternatingItem Then
Dim myKnowledgeScorecmb As DropDownList = item.FindControl("cmbKnowledgeScore")
myKnowledgeScorecmb.ID = "cmbKnowledgeScore" + item.DataItem("idSkillAudit").ToString
myKnowledgeScorecmb.SelectedValue = item.DataItem("KnowledgeScore")

Dim myUsageScorecmb As DropDownList = item.FindControl("cmbUsageScore")
myUsageScorecmb.ID = "cmbUsageScore-" + item.DataItem("idSkillAudit").ToString
myUsageScorecmb.SelectedValue = item.DataItem("UsageScore")

Dim myIDHiddenField As HiddenField = item.FindControl("IDHiddenField")
myIDHiddenField.Value = item.DataItem("idSkillAudit")


End If
End Sub




 
1st thing... get rid of the lines where you append data to the id of the control. this isn't needed.

2nd. there doesn't seem to be anything wrong with this code, but if you don't check for postback in the page_load event this could be causing the problem. the code should looks something like this
Code:
page_load(...)
{
   if(IsPostBack == false)
   {
      MyRepeater.DataSource = GetDataFromSomewhere();
      MyRepeater.DataBind();
   }
}
if your code looks like this

Code:
page_load(...)
{
   MyRepeater.DataSource = GetDataFromSomewhere();
   MyRepeater.DataBind();
}
the problem is you are overwriting the user input with each post back.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Hi,

i don't load the data in the page_load, using ObjectDataSources at design time, i will look at binding them during load time then checking for postback.

Problem is though when the page reloads after pressing save, it does not show any data as if the ODS's are not pulling any data back

 
Hwy Jason,

i think i may of solved it, taking the code appending the ID to the fieldID made it all work.

Got to do more testing and write the save code now, but looking good, your help and guidance has been much appreciated.


Mark


 
Sorry meant to say taking the ID code out, made it work

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top