obtaining the value of a dropdown control inside a gridview control

Hi,

I am working on a site to process assessments against a particular business service. The assessments have 8 stages and there are questions for each stage with a list of responses. I am experimenting with different user interfaces for doing this and am open for suggestions on the best way, but for the moment I have a page for each assessment with a dropdown list containing stage 1-8, on selecting a stage a gridview is built containing the questions and responses for that particular stage. So the first column is the question and the second column a drop down list of possible responses, on loading a particular assessment these responses should be set to the current values, but what I want the user to do is complete each stage of the assessment, changing the responses by the drop down and then submit, which will post the updates back. I am finding it difficult to reference the drop down list control inside each row. As mentioned if anyone has any ideas on doing this a different easier way then please feel free to suggest.

 

 

0
babymonkey
3/30/2009 8:18:24 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

7 Replies
1105 Views

Similar Articles

[PageSpeed] 16

You can loop through each row dropdown like below :

For i = 0 to gridview1.rows.count - 1

 CType(gridview1.Rows(i).FindControl("DropdownId"), DropDownList).SelectedItem.Value

Next

0
mcr_subbu
3/30/2009 9:30:33 AM

Hi,

Please note that FindControl method is not recursive, so when accessing controls in GridView using FindControl then be sure to specify its Row and Cells index to avoid null reference exceptions.. Here's an example:

 

    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            //just the change the index of cells to where your DDL resides
            DropDownList ddl = (DropDownList)GridView1.Rows[i].Cells[0].FindControl("DropDownList1");
            if (ddl != null)
            { 
                //do something
            }
        }
    }
  
Regards,Vinz

"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
0
vinz
3/30/2009 9:50:25 AM

Hi, thanks for your help however I have the following couple of problems

A) If I loop through the gridview1 after pressing a button control with the 'autopostback' applied like this:-

For i = 0 To grid.Rows.Count - 1Dim answer As String = CType(grid.Rows(i).FindControl("responsesDDL"), DropDownList).SelectedItem.Value

Response.Write(answer)

Next

The resposnes are the default ones in the database, that is, if I change the drop down response the answer writen with response.write is the original answer not my new one, also the postback sets the drop down back to the original value. The original answers are also printed out this way if I configure a post back url and access the gridview drop down from the previous page object.

B) I need to collect all the responses for each stage, therefore with my stage drop down selection i can only get back the answers from each stage as the gridview1 is built and populated only from the questions for that particular stage. I need to collect answers from the questions in all stages and then post them to another page.

 

0
babymonkey
3/31/2009 2:58:08 AM

bueler... bueler....

 

 

 

0
babymonkey
4/2/2009 3:41:50 AM

babymonkey:
The resposnes are the default ones in the database, that is, if I change the drop down response the answer writen with response.write is the original answer not my new one, also the postback sets the drop down back to the original value. The original answers are also printed out this way if I configure a post back url and access the gridview drop down from the previous page object.
 

This happens when you are not binding your GridView on initial postbacks.. What I mean to say is if you are binding your Grid manually then you should bind your GridView within NOT IsPostback block so that the DropDownList state will  not reset on postbacks..

babymonkey:
B)  I need to collect answers from the questions in all stages and then post them to another page.
 

You can add the list of all answers in an ArrayList or in a StringCollections and then  store the values of ArrayList in Session variable so that you can gain access to it to another page.


Regards,Vinz

"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
0
vinz
4/2/2009 3:50:10 AM

 

1    
  

Hi, thanks for the response and sorry for my tardy reply. The thing is, I need to make the gridview bind on a postback as I need to fill it with the relevant stages questions. The gridview is driven from a stage drop down (as below). Therefore for each stage, i need to collect the responses (and the responseid field, not the text) for each stage and then submit. Just wondering how I could accomplish this. Thank you again for your help so far. I really do appreciate it.

 

getresponses2.aspx

1    &lt;%@ Page Language="VB" AutoEventWireup="false" CodeFile="getresponses2.aspx.vb" Inherits="getresponses2" %&gt;
2    
3    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4    
5    <html xmlns="http://www.w3.org/1999/xhtml">
6    <head runat="server">
7        <title></title>
8    </head>
9    <body>
10       <form id="form1" runat="server">
11       <div>
12       <h1>Questions & Responses</h1>
13       <p>
14       <asp:GridView ID="gridview1" runat="server"
15       AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" 
16               BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" 
17               style="z-index: 1; left: 14px; top: 215px; position: absolute; height: 205px; width: 955px">
18           <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
19       <columns>
20       <asp:BoundField DataField="Question"
21       HeaderText="Question" />
22       <asp:TemplateField HeaderText="Responses">
23       <ItemTemplate>
24       <asp:DropDownList ID="responsesDDL" runat="server" Datasource="<%# CType (CType(Container.DataItem,System.Data.DataRowView).Row,riskwheelDS.questionsRow).Getresponses() %>"
25       DataTextField="Response">
26       </asp:DropDownList>
27   
28       </ItemTemplate></asp:TemplateField>
29       </columns>
30           <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
31           <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
32           <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
33           <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
34           <AlternatingRowStyle BackColor="#DCDCDC" />
35   </asp:GridView>
36       </p>
37       </div>
38       <div style="z-index: 1; left: 11px; top: 457px; position: absolute; height: 65px; width: 86px">
39           <asp:Button ID="Button1" runat="server" 
40               style="z-index: 1; left: 13px; top: 23px; position: absolute" Text="Button" />
41       </div>
42       <asp:DropDownList ID="spmsStageDDL" runat="server" AutoPostBack="True" 
43           style="z-index: 1; left: 19px; top: 116px; position: absolute">
44           <asp:ListItem Value="1">SPMS 1</asp:ListItem>
45           <asp:ListItem Value="2">SPMS 2</asp:ListItem>
46       </asp:DropDownList>
47       </form>
48   </body>
49   </html>

getresponses2.aspx.vb

1    Imports riskwheelDSTableAdapters
2    Partial Class getresponses2
3        Inherits System.Web.UI.Page
4    
5        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
6            Dim stageNo As String = spmsStageDDL.SelectedValue
7            If Not IsPostBack Then
8                Dim questionsAdapter As New questionsTableAdapter
9                gridview1.DataSource = questionsAdapter.GetquestionsBystageNo(stageNo)
10               gridview1.DataBind()
11           End If
12           
13       End Sub
14   
15       Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
16           For i = 0 To gridview1.Rows.Count - 1
17               Response.Write("&lt;br /&gt;")
18               Response.Write(CType(gridview1.Rows(i).FindControl("responsesDDL"), DropDownList).SelectedItem.Value)
19   
20           Next
21       End Sub
22   End Class
 
0
babymonkey
4/14/2009 4:20:44 AM

Hi can anyone help me on this?

 

 

0
babymonkey
4/20/2009 4:02:33 AM
Reply:

Similar Artilces:

Getting the control id of a control inside gridview control
 uhmm.. a little bit confusing regarding my subject because i dont know how to say it...by the way my problem is how can i get the id of a control (e.g. textbox) inside the gridview... and the textbox is in the templatefield... is there any way i can get its id??  hoping i could get a reply as soon as possible... Regards,MhaeyPlease remember to click “Mark as Answer” on the post that helps you.. =) What do you want's to do by getting the id of the Text box? Do you want's to find particular Textbox with certain id ? Kamran ShahidSr. Software Engineer(MCP,MCAD.net,MC...

Help Req: !!! :- On Disabling Controls Present inside a Gridview control
Hi All, We have A requirement Where we have disable controls inside a grid.These controls can be textbox, DropDownlist (in TemplateField), ComandField present inside the grid.We have to call a generic method for doing the same .(since it can be done for multiple grid.) PFB the code snippet we use. Which disables all the controls inside the page.The bolded part of the code is used to loop inside a grid view.(Not Working.)Is this ideal way to deal with the requirement.Or can someone suggest be a better way.        public void DisableMe(Page p1, st...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

Custom Control within a Control Control
I am developing a custom controls that will render headers for different web sites. Within these controls, I want to place another custom control that will change the language. I created the language control and had it implement IPostBackDataHandler. This control works fine if I drag it on a new web form, but if I tell a header custom control to render it problems occur. The Language Selector custom control does not have a Page member anymore and the event handlers do not work. Does anyone have any experience with doing something similiar? Anyone have any suggestions? Thanks, ...

How do I control one control from another control?
I have a page page1.aspx which is within a master page. On page I have a table with two cells. Within each cell I have placed two controls (ascx) that I have developed. Now I'd like to be able to get these two controls to interact. One needs to update the other. One has some search criteria that the other one needs to update its gridview. One needs to make the other visible/not visible at certain times. Out of the box neither control can see each other at all. Is there a good article or someplace I can find some information on how to do this?  AllAboutFocus.com. Information Technolo...

Control.Value and Control.SelectedItem.Value
Can someone explain to me the difference between say, Dropdownlist.SelectedItem.Value and Dropdownlist.SelectedValue?? Just curious, Thanks!Tim These are going to return the same thing. dropdownlist.selectedvalue is a shortcut to get the currently selected value in the dropdown. If that is all you want then you can just use that. If you want to access the currently selected item you can use dropdownlist.selecteditem and that return the item and you can then access any of the item's properties, including in this case the value property. If this post answered your question please r...

gridview control in another gridview control
Hi ia have a questioni have a gridview control which contains button in one colum.now my question is i need another gridview to be placed in the  first gidviewso that when i click on the button the second gridview will populate the data.now my question is how to place second gridview in first gridview.or is there any approach like first grid itself acting as parent and child.please help me  Hi sirikalavalapalli:                I think nested gridview will help you. Check this link and it will show you how...

Cannot access a dropdownlist control inside the gridview control
I am using the Gridview control without a DataSource control. When I click on the Edit button, the page posts back and enters the following code: <code>     Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing         Try            Dim row As GridViewRow = GridView1.Rows(e.NewEditIndex)            Dim ddlCustomerID As DropDownList...

when loading a previously created record on to form, some controls are missing values(controls are depended on values selected in other controls)
Hello - I have created a page for Editing records.  this page will open up a chosen record and allow user to edit the data.On the form, I have 3 drop down lists.   The second dropdown list should fill values depending on value chosen in first dropdown list.  The third dropdown list should show/fill values depending on value chosen in second dropdown list.So taking this into count, i have several records with various data.  When I open-up/load a record in Edit form, only the first dropdown list has a value selected that was the pre-existing va...

Cascading Dropdown error in formview controls and finding all controls in formview controls
can help with the following? Instantiate Cascading Dropdowns within Formview controls (this returns the error message "databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control".) Pass values between controls within master controls such as Textboxes within formviews (example: Pass the selected value in a dropdownlist to another textbox both WITHIN a formview control) Expose the public properties of a master page Hi arraybiz, 1) I need your code. See sample: http://www.mikepope.com/blog/fun/cascadingdropdowns_dy...

TextBox control not retaining values inside Wizard Control
I might be doing someting very silly. I have a wizard control. Everthing works fine on Wizardstep 1 . But on the step 2 I have some TextBoxes and a Button. When I click on the button a page postback happens and then there is nothing in the TextBox.Text property in the code behind.   Thanks I notice the status of this thread is “Resolved”, is it marked yourself? If so, please post your answer in order for others member learn from it.  We will greatly appreciate all your sharing.Hong-Gang ChenMicrosoft Online Community SupportPlease remember to mark the replies as answers if the...

Not able to assing values when controls inside Tab control
Hi I am having a TabContainer and 4 tabs, inside each tab i have a update pannel and a gridview, the grid has an select command text, and some other coloumns. i have 2 text box out side the tabcontainer. so that the user selects an row in grid, the selected row's is displayed in the text box exg the grid has select   aaa    12     15105719    abc2 select   bbb    62     15342349    abc3 select   ccc    52   &nbs...

controls inside a listView control
 I'm using a listview control.Inside the template I have a checkbox.Everything is inside an updatepanel. When a checkbox is checked or unchecked I want to trigger my AsyncPostBack.It works as it should when I have one manually added checkbox with no listView. However when I use a listView it will not run my event at all. If I check the checkbox it goes about 5 seconds and it unchecks again, no postback made.Here's the code I use... protected void cbCheck_CheckedChanged(object sender, EventArgs e)    {        CheckBox cb = (C...

controlls inheriting values from other controlls
hi, on my page www.ffe-clan.com/email2.aspx i am trying to make the textbox TO (id=txto) inherit the selected values from the datalist box (id=emails) next to it. is there any way i could do this? thanks for any help given i would think that in your postback event, you would simply get all selected values from the list and append them together (comma seperated) and then fill in the text property of the textbox.Mike Banavige~~~~~~~~~~~~Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/ thanks, but what kind of code would i have t...

Web resources about - obtaining the value of a dropdown control inside a gridview control - asp.net.presentation-controls

SH prosecutors charge GSK-related investigators with illegally obtaining info
SH prosecutors charge GSK-related investigators with illegally obtaining info

ABQjournal: Obtaining Cheney Rally Ticket Requires Signing Bush Endorsement
Some would-be spectators hoping to attend Vice President Dick Cheney's rally in Rio Rancho this weekend walked out of a Republican campaign office ...

Gay Teacher Fired After Obtaining Marriage License
A 12-year-teaching veteran was fired from a Catholic high school in Pennsylvania immediately after he applied for a marriage license with his ...

Apple Outlines The Legal Process For Obtaining User Data In New Document
Apple is offering both users and law enforcement officials more information on how and when user data can be requested.

Two plead guilty to fraudulently obtaining government benefits in Cayuga County
The Cayuga County District Attorney's Office said that two people pleaded guilty Thursday to fraudulently receiving government funds.

"And so really the only argument you can make against the verification and inspection mechanism that ...
"... because they’d find some way to get around it because they're untrustworthy. And if that's your view, then we go back to the choice that ...

Obtaining Business Loans From Neighbors
In January 2010, Ben Slayton and his wife, Erin Cinelli, tapped personal savings and borrowed money from their parents to buy a butcher shop ...

Court docs: Toronto mayor tried obtaining tape
TORONTO (AP) â" New court documents released Wednesday suggest Toronto Mayor Rob Ford may have offered $5,000 and a car to suspected drug dealers ...

Obtaining 25% Of Energy From Renewables Would Save Billions
... plants, according to a comprehensive new analysis released today by the National Renewable Energy Laboratory (NREL). The study found that obtaining ...

Same sex couples in Texas begin obtaining marriage licenses
As some Texans celebrated the marriage equality announcement in the state capital, the fight may not be entirely over. Gov. Greg Abbott issued ...

Resources last updated: 12/18/2015 3:03:09 PM