Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8987025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:41:30+00:00 2026-06-15T21:41:30+00:00

I have a gridview in an ASP.NET site. There’s a TemplateField right now that

  • 0

I have a gridview in an ASP.NET site.

There’s a TemplateField right now that works fine – this is a survey form, and by default questions are answered using textboxes:

<asp:TemplateField HeaderText="" Visible="True" >
   <ItemTemplate>
      <asp:TextBox ID="txtAnswer" Text='<%# Bind("Answer") %>' runat="server" 
       TextMode="MultiLine" Height="76px" MaxLength="2000" Width="377px">
       </asp:TextBox>
    </ItemTemplate>
 <EditItemTemplate>
       <asp:TextBox ID="txtAnswer" Text='<%# Bind("Answer") %>' runat="server" 
       TextMode="MultiLine" Height="76px" MaxLength="2000" Width="377px">
       </asp:TextBox>
 </EditItemTemplate>
 </asp:TemplateField>

The requirement is to now add new questions that may be answered by dropdownlists. I’ve got an idea on how to do this (passing the question number into a function and checking for the presence of dropdown answers that are stored in another table), but I’m struggling with dynamically updating the templatefield based on the question type…any suggestions?

UPDATE

I managed to get this partially working through a helper function. (Probably not my best code, but it’s almost getting the job done) The thing is I get a string that’s telling me the type in my output instead of the actual controls (Textboxes or dropdownlists, respectively)…how can I correct that?

  public Control GetAnswerControl(string QuestionID, string Answer)
{
    List<ListItem> lstOptions = new List<ListItem>();

    SqlCommand cmd = new SqlCommand("pDropDownAnswers_Get", functions.NewSupplierRequest);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@QuestionID", SqlDbType.Int);
    cmd.Parameters["@QuestionID"].Value = Int32.Parse(QuestionID);

    try
    {
        functions.NewSupplierRequest.Open();
        SqlDataReader r = cmd.ExecuteReader();
        while (r.Read())
        {
            lstOptions.Add(new ListItem(r["DropDownAnswer"].ToString(), r["DropDownAnswer"].ToString()));
        }
    }
    catch (Exception err)
    {
        this.lblError.Text = err.Message;
    }
    finally
    {
        functions.NewSupplierRequest.Close();
    }

    if (lstOptions.Count == 0)
    {
        TextBox tb = new TextBox();
        tb.ID = "txtAnswer";
        tb.Text = Answer;
        tb.TextMode = TextBoxMode.MultiLine;
        tb.Height = 76;
        tb.Width = 377;
        tb.MaxLength = 2000;

        return tb;
    }
    else
    {
        DropDownList dl = new DropDownList();
        dl.DataSource = lstOptions;
        dl.DataBind();
        dl.SelectedValue = Answer;
        return dl;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:41:32+00:00Added an answer on June 15, 2026 at 9:41 pm

    I wound up getting this to work with a different approach…

    1. First I set some hidden fields and a placeholder in the gridview to hold the question ID and answer ID
    2. Then I created a helper function to loop through the rows of the gridview, grabbing the ID and answer from the hidden fields.
    3. Adding the controls to the placeholder from the codebehind actually formats them as the controls, not the string that I was getting.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any template site out there that has templates utilizing specifically:Asp.net button,asp.net gridview,
I have a Jquery Accordion and I have a asp.net GridView below that. When
I have gridview in my asp.net 3.5 application [C#]. Which looks like this: <asp:GridView
I have a site using ASP.NET Membership and have a number of roles that
i have followed GridView in my ASP.Net Page: http://s2.imgimg.de/uploads/14b706b38JPG.jpg It should look like this:
i have an issue with logic in ASP.NET web site - (gridview - filter
I have a GridView control on my ASP.NET page that binds to result set
I have a gridview control in ASP.Net 2.0 and i need to export this
Hy, I have a gridView control in Asp.NET like this: <asp:GridView ID=outputGridView runat=server onrowediting=OutputGridView_RowEditing>
OK, so this is bugging me. I have a gridview in ASP.net which I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.