1

Here's a picture of the gridview I'm working with: https://i.sstatic.net/Ce5gG.png

Here's my issue: I have a DropDownList (not shown in the picture) that has the same number of items as the Fraction DropDownList (shown in image). Each of the indices match with each other (ex: index 4 of the other field is the same as index 4 of the Fraction DropDownList), so when submitting the form, they have to have both of these match properly.

What I want to do is check and see if they do indeed match. I've tried:

this.gvRecords.Rows[0].Cells[2].Text

&

(((DropDownList)this.gvReserveRecords.Controls[0].Controls[0]
.FindControl("gvcbnFraction")).SelectedIndex

Neither have worked. They are just returning an empty string.

Here's some info that might help:

  • I am using ASP.NET Web Forms

  • The GridView is initially empty, but once the information is filled in, the 'Insert' LinkButton is clicked and the data is added to a database, and the GridView then calls DataBind().

  • I'm trying to compare the value of Fraction to a field outside of the GridView, if that matters at all.

Let me know if you need any other information!

4
  • 1
    Why are you trying to find the dropdown by going through the GridMenu? It has an ID, right? Just go gvcbnFraction.SelectedIndex() or gvcbnFraction.SelectedValue() Commented Jul 25, 2012 at 21:21
  • <asp:DropDownList ID="gvcbnFraction" runat="server" Width="75px"> <asp:ListItem>FLOAT</asp:ListItem> <asp:ListItem>1/8</asp:ListItem> ...</asp:DropDownList> That's how it looks on the ascx page, however, I can't use it in my codebehind file for some reason. It tells me that it's not available in the current context. Commented Jul 25, 2012 at 21:29
  • I suspect that the dropdown isn't registered correctly in the designer file, or you have some references missing between the .aspx and the .cs files. Try dragging a DropDownList control from the toolbox onto your aspx page and then see if you can reference it in the code-behind by the default ID - DropDownList1 edit: Sorry, when I say toolbox I assume you are using Visual Studio Commented Jul 25, 2012 at 21:49
  • Well the issue is that the DropDownList only shows up in the webpage it self. In Visual Studio, it's in a gridview, so the designer only shows the gridview... All the dropdownlists that are outside the gridview can easily be accessed like that... Commented Jul 25, 2012 at 22:02

1 Answer 1

1

You should be able to simply reference your DropDown control by its ID, in this case - gvcbnFraction. So, gvcbnFraction.SelectedIndex should give the integer index and gvcbnFraction.SelectedValue should give the string value. If you aren't able to reference the drop down control in your code behind, then you are missing something that should be there. Possibly one of these:

  1. Your designer.cs file doesn't include protected global::System.Web.UI.WebControls.DropDownList gvcbnFraction;
  2. Your .aspx file doesn't include CodeBehind="MySuperAwesomePage.aspx.cs" Inherits="MyWebApp._Default" or something similar to that
  3. Something else I just don't know about

If you are using Visual Studio, you can try dragging a control from the toolbox and see if you can reference it. Doing this will automatically update your designer.cs file, eliminating #1

Sign up to request clarification or add additional context in comments.

1 Comment

1. It's not in the designer file, but there are other dropdownlists that are in the cs file which are not in the designer file which work fine. 2. My .ascx file includes the codebehind file properly. 3. I hope not. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.