locked
repeater control textbox does not show the applied value RRS feed

  • Question

  • User1182587605 posted

    In my page, I have a gridview and a repeater control. If I select one column value from the popup gridview, The selected text(string) should be placed into the repeaters first empty column textbox. I have written the code and while debug, My string value takes the gridview selected value but the string value is not being applied in the repeater textbox. Please check my code and help me how to do this.

    gridview ASPX

     <div id="Div1" runat="server" style="max-height: 350px; width: 900px; overflow: auto;" align="center">
                                                                                <br />
                                                                                <asp:GridView ID="GridView1" OnRowCommand="GridView1_RowCommand" runat="server" AutoGenerateColumns="false" Font-Size="Small" HeaderStyle-BackColor="#3C5569" HeaderStyle-ForeColor="White">
                                                                                    <Columns>                                                                                    
                                                                                       
                                                                                        <asp:TemplateField HeaderText="ItemNumber">
                                                                                            <ItemTemplate>
                                                                                                <%--<asp:HyperLink runat="server" ID="ItemNumber" Text='<%# Eval("ItemNumber") %>' HeaderText="ItemNumber" ItemStyle-Width="80" DataNavigateUrlFields="ItemNumber"></asp:HyperLink>--%>
                                                                                                <asp:LinkButton ID="lnkbtnItemNumber" CommandName="Select" CommandArgument='<%# Eval("ItemNumber") %>' runat="server" Text='<%# Eval("ItemNumber") %>' OnClick="lnkbtnItemNumber_Click" ItemStyle-Width="80" DataNavigateUrlFields="ItemNumber"></asp:LinkButton>
                                                                                            </ItemTemplate>
                                                                                        </asp:TemplateField>
                                                                                        <asp:BoundField DataField="Variation" HeaderText="Variation" ItemStyle-Width="280" />
                                                                                        <asp:BoundField DataField="ItemName" HeaderText="ItemName" ItemStyle-Width="250" />
                                                                                        <asp:BoundField DataField="SellingPrice" HeaderText="Cost($)" ItemStyle-Width="80" />
                                                                                    </Columns>
                                                                                </asp:GridView>
                                                                            </div>
    
    

    Repeater ASPx:

    <asp:Repeater ID="Repeater2" OnItemCommand="Repeater2_ItemCommand" OnItemDataBound="Repeater2_ItemDataBound" runat="server">
                                                                <ItemTemplate>
                                                                    <tr>                                                                   
                                                                        <td>                                                                        
                                                                            <asp:TextBox ID="ItemId" onfocus="moveup(this);" class="form-control" runat="server" AutoPostBack="False" Width="130" Text=''></asp:TextBox>
                                                                        </td>
                                                                        <td>
                                                                            <asp:TextBox ID="variation" onfocus="moveup(this);" class="form-control" AutoPostBack="true" OnTextChanged="ItemId_TextChanged" runat="server" Text='' Width="200"></asp:TextBox>
                                                                        </td>
                                                                        <td>
                                                                            <asp:TextBox ID="ItemName" class="form-control" runat="server" Text='' ReadOnly="True" Width="300"></asp:TextBox>
                                                                        </td>
                                                                        <td>
                                                                            <asp:TextBox ID="Qnty" onfocus="moveup(this);" runat="server" Text='' OnTextChanged="Qnty_TextChanged" AutoPostBack="true" Width="100" Style="text-align: right" class="form-control" TabIndex="2"></asp:TextBox>
                                                                        </td>
                                                                        <td>
                                                                            <asp:TextBox ID="Cost" class="form-control" runat="server" Text='' Width="100" Style="text-align: right" ReadOnly="True"> </asp:TextBox>
                                                                        </td>
                                                                        <td>
                                                                            <asp:TextBox ID="Value" runat="server" class="form-control" Text='' Width="150" Placeholder="AutoCalculated" Style="text-align: right" ReadOnly="True" TabIndex="0"></asp:TextBox>
                                                                        </td>                                                                    
                                                                    </tr>
                                                                </ItemTemplate>
                                                                <FooterTemplate>
                                                                    <tr>
                                                                        <td>
                                                                            <asp:Label runat="server" class="control-label col-sm-3 col-md-3 col-lg-3" ID="Label13" Width="130" Style="text-align: center" Text="" Font-Size="Small"></asp:Label></td>
                                                                        <td>
                                                                            <asp:Label runat="server" class="control-label col-sm-3 col-md-3 col-lg-3" ID="Label29" Width="200" Style="text-align: center" Text="" Font-Size="Small"></asp:Label></td>
                                                                        <td>
                                                                            <asp:Label runat="server" ID="Label14" Style="text-align: right" class="" Text="" Width="300" Font-Size="Small"></asp:Label></td>
                                                                        <td>
                                                                            <asp:Label runat="server" ID="Label15" Style="text-align: right" class="" Text="" Width="100" Font-Size="Small"></asp:Label></td>
                                                                        <td>
                                                                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <asp:Button runat="server" ID="Button2" Text="Save" class="btn btn-dark-blue" Style="text-align: center" Width="100" CommandName="UpdateDatabase" OnClick="Button2_Click" TabIndex="1000"></asp:Button>
                                                                        </td>
                                                                    </tr>
                                                                </FooterTemplate>
                                                            </asp:Repeater>

    CS code:

     protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "Select")
                {
                    //((TextBox)item.FindControl("ItemId")).Text = lnkbtnItemNumber.Text;
                    //((TextBox)item.FindControl("ItemId")).Text = GridView1.FindControl("lnkbtnItemNumber").ToString();
                    //((TextBox)Repeater2.FindControl("ItemId")) = GridView1.FindControl("lnkbtnItemNumber").ToString();
    
                    string selecteditem = Convert.ToString(e.CommandArgument);
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "closeModal();", true);
                    //((TextBox)item.FindControl("ItemId")).Text = selecteditem;
                    ((TextBox)Repeater2.Items[0].FindControl("ItemId")).Text = selecteditem;
                    ((TextBox)Repeater2.Items[0].FindControl("ItemId")).Focus();
                }
             }
    
     protected void Page_PreRender(object sender, EventArgs e)
            {
                if (IsPostBack)
                {
    
                    WebControl ctrl = GetPostBackControl() as WebControl;
                    if (ctrl != null && !SetNextFocus(Controls, ctrl.TabIndex + 1)) { ctrl.Focus(); }
                }
                if (!IsPostBack)
                {
    
                }
                else
                {
                    if (Repeater3.Visible == true)
                    {
                        Repeater1.Visible = true;
                    }
                }
            }
    
    public Control GetPostBackControl()
            {
                Control control = null;
    
                string ctrlname = Request.Params.Get("__EVENTTARGET");
                if (ctrlname != null && ctrlname != string.Empty)
                {
                    control = FindControl(ctrlname);
                    control.Focus();
                }
                else
                {
                    foreach (string ctl in Request.Form)
                    {
                        Control c = FindControl(ctl);
                        if (c is Button)
                        {
                            control = c;
                            break;
                        }
                    }
                }
                return control;
            }
    
    private bool SetNextFocus(ControlCollection controls, int tabIndex)
            {
                foreach (Control control in controls)
                {
                    if (control.HasControls())
                    {
                        bool found = SetNextFocus(control.Controls, tabIndex);
                        if (found) { return true; }
                    }
    
                    WebControl webControl = control as WebControl;
                    if (webControl == null) { continue; }
                    if (webControl.TabIndex != tabIndex) { continue; }
    
                    webControl.Focus();
                    return true;
                }
    
                return false;
            }

    Right now, The string variable contains the selected value which is "C123" but the same value is not being pushed into the repeater2 "itemid" textbox column. Even after I set the value:

    ((TextBox)Repeater2.Items[0].FindControl("ItemId")).Text = selecteditem;

    Please help me in this.

    Regards,

    Deepak

    Thursday, July 27, 2017 8:20 AM

All replies

  • User-2010311731 posted

    Could you show the code where you are binding data to the Repeater?  I don't see any data bound items in your repeater, so I am not sure how you are populating it.

    Matt

    Thursday, July 27, 2017 7:22 PM
  • User1182587605 posted

    Hi Matt,

    In my case,This is a data entry page. I need to fill in the data from the selected contents of the Gridview. But still, When I want to bind my data, I am using the below code. But I need the Repeaters first empty itemid textbox to be filled with the selected value from the GridView.

    Please check the repeater control databinding code below:

    private void SetInitialRow()
            {
                DataTable dt = new DataTable();
                DataRow dr = null;
                if (txtNoofItems.Text == "")
                {
                    Repeater2.DataSource = dt;
                    Repeater2.DataBind();
                }
                else
                {
                    int a = Convert.ToInt32(txtNoofItems.Text);
                    dt.Columns.Add(new DataColumn("ItemName", typeof(string)));
                    dt.Columns.Add(new DataColumn("Qnty", typeof(string)));
                    dt.Columns.Add(new DataColumn("Cost", typeof(string)));
                    for (int i = 0; i < a; i++)
                    {
                        dr = dt.NewRow();                
                        dr["ItemName"] = string.Empty;
                        dr["Qnty"] = string.Empty;
                        dr["Cost"] = string.Empty;                   
                        dt.Rows.Add(dr);
                    }
                }
                if (Repeater2.Visible == false)
                {
                    Repeater2.Visible = true;
                }
                ViewState["CurrentTable"] = dt;
                Repeater2.DataSource = dt;
                Repeater2.DataBind();
            }
    

    I call this method in a for loop to populae the values in my repeater.

    Regards,

    Deepak

    Thursday, July 27, 2017 7:52 PM
  • User-1838255255 posted

    Hi acmedeepak,

    According to your description and code, I tested it in my side, I found it could get value from gridview then set it to the repeater. So I have some suggestion to you:

    1. Add a break point in this event, check if get this value.

    2. Check if get this control "ItemId".

    3. You say can not set it value, if meet some exception value or error message, please post it, this will better help you to solve the problem.

    Best Regards,

    Eric Du

    Friday, July 28, 2017 8:52 AM
  • User1182587605 posted

    I do not have I think my below code is making the gridview value not to come up i the repeater. Please check the code and help me how to modify it so that I can get my task done.

    public Control GetPostBackControl()
            {
                Control control = null;
    
                string ctrlname = Request.Params.Get("__EVENTTARGET");
                if (ctrlname != null && ctrlname != string.Empty)
                {
                    control = FindControl(ctrlname);
                    control.Focus();
                }
                else
                {
                    foreach (string ctl in Request.Form)
                    {
                        Control c = FindControl(ctl);
                        if (c is Button)
                        {
                            control = c;
                            break;
                        }
                    }
                }
                return control;
            }
    
    private bool SetNextFocus(ControlCollection controls, int tabIndex)
            {
                foreach (Control control in controls)
                {
                    if (control.HasControls())
                    {
                        bool found = SetNextFocus(control.Controls, tabIndex);
                        if (found) { return true; }
                    }
    
                    WebControl webControl = control as WebControl;
                    if (webControl == null) { continue; }
                    if (webControl.TabIndex != tabIndex) { continue; }
    
                    webControl.Focus();
                    return true;
                }
    
                return false;
            }

    Please help me how I can do this.

    Regards,

    Deepak

    Monday, July 31, 2017 6:04 AM
  • User1182587605 posted

    Hi Eric,

    I am still stuck with the same issue. Could you please help me fix this. I have tried debugging the code but I am unable to solve this issue. Please help me in this. 

    Thanks,

    Deepak

    Wednesday, August 2, 2017 5:19 AM
  • User991499041 posted

    Hi Deepak,

    You could use below code to change textbox value in repeater control.

    foreach (RepeaterItem item in this.Repeater2.Items)
    {
        ((TextBox)item.FindControl("ItemId")).Text = selecteditem;
    }

    Regards,

    zxj

    Wednesday, August 9, 2017 8:42 AM