0

Here is my aspx page : i want to bind a column to Dropdownlist but i cant find Repeater("Repeater1") in back-end code. Please Help !

   <asp:GridView ID="gridViewAllotment" CssClass="table table-striped" runat="server" 
                        AutoGenerateColumns="false" GridLines="None" BorderColor="#999999" 
                        BorderStyle="Groove">
                        <Columns>
                        <asp:TemplateField HeaderText="Traffic Police">
                            <ItemTemplate>
                                <%# Eval("U_Name") %>
                            </ItemTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Traffic Junction">
                         <ItemTemplate>

                       <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound = "Test">
                            <ItemTemplate>
                            <asp:DropDownList ID="ddl_TrafficJunction" runat="server" DataTextField="Junction">            
                            </asp:DropDownList>
                          </itemTemplate>
                        </asp:Repeater>                          
                        </itemTemplate>                            
                        </asp:TemplateField>
                  </Columns>
                </asp:GridView>        

Here is the Back end code:

public partial class JunctionAllotment : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { gridViewAllotment.DataSource = AllotmentLogic.SelectAllTrafficPolices();
gridViewAllotment.DataBind();

    Repeater1.DataSource = RepSource();
    Repeater1.DataBind();
}

public List<string> RepSource()
{
    DataTable data = (DataTable)AllotmentLogic.SelectAllTrafficPolices();
    var j = data.Rows.Count;
    List<string> rep = new List<string>();
    for (int i = 0; i < j ; i++)
    {
        rep.Add(i.ToString());
    }
    return rep;
}

public void FillDropdown(DropDownList ddl)
{
    DataTable dt = AllotmentLogic.SelectTrafficJunction();

    foreach (DataRow row in dt.Rows)
    {
        if (row["TrafficJunction_Name"].ToString() != null)
        {
            ddl.Items.Add(row["TrafficJunction_Name"].ToString());
        }
    }
}

In backend code it says "Repeater1" does not exists in current context.

2
  • How about something like this: stackoverflow.com/questions/3310574/asp-repeater-databound Commented Apr 17, 2016 at 19:31
  • My problem is repeater getting data when i use it outside the grid but its not working inside the grid even after using itemtemplate. Commented Apr 18, 2016 at 9:07

1 Answer 1

0

This piece of code is highly suspect. This code says the GridView needs one or more DropDownLists for each row

    <asp:TemplateField HeaderText="Traffic Junction">
      <ItemTemplate>
        <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound = "Test">
          <ItemTemplate>
            <asp:DropDownList ID="ddl_TrafficJunction" runat="server"
                              DataTextField="Junction">            
            </asp:DropDownList>
          </ItemTemplate>
        </asp:Repeater>                          
      </ItemTemplate>                            
    </asp:TemplateField>

I think this is more inline with what you're trying to do:

    <asp:TemplateField HeaderText="Traffic Junction">
      <ItemTemplate>
          <asp:DropDownList ID="ddl_TrafficJunction" runat="server"
                            DataTextField="Junction">            
          </asp:DropDownList>
      </ItemTemplate>                            
    </asp:TemplateField>

Then in the code behind(Sorry for the VB - but C# should be simple enough to convert)

    Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
      If e.Row.RowType = DataControlRowType.DataRow Then
        Dim ddl As DropDownList = e.Row.FindControl("ddl_TrafficJunction")

        ddl.DataSource = CType(AllotmentLogic.SelectTrafficJunction(), DataTable)
        ddl.DataBind()
      End If
    End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

I need a DropDownList for each row in grid.
Thanks a lot for the help.. I did it :)
Excellent. Good Job.

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.