0

I am about to develop a web application which is used to search and fetch the data from the database as per the criteria which we enter in textboxes.

I have from place and to place once we enter the data in either textbox and hit search button it will fetch data from database and display in textbox (dynamic).

Eg:

if I enter a place kolkata and hit enter then it will count the total no of records that has the from place kolkatta. If count is 3 then 3 textboxes, 3 buttons (add) and 3 gridviews should be created dynamically. Then if we click the add button it will redirect to next page.

I have created a textbox and a button but I'm unable to create 3 gridviews and also click event for the button is not firing.

Here is my code for creating controls dynamically on search button click:

protected void BTNSearch_Click(object sender, EventArgs e)
{
            if(Convert.ToInt32( Session["Count"])>0)
            {
                if (Flag != "")
                {
                    LoadControls(Flag);
                }
            }
}

private void LoadControls(string Flag)
{
            LocationName.Clear();
            Session["Flag"] = Flag;
            Updatesearch.Update();
            dynamicTextBoxes = new TextBox[Convert.ToInt32(Session["Count"])];
            dynamicButtons = new Button[Convert.ToInt32(Session["Count"])];
            dynamicGV = new GridView[Convert.ToInt32(Session["Count"])];
            int i;

            if (Flag == "L")
            {
                sql = "Select PortOfDischargeName from VW_TransAIHBLMasterUpdateDetails  where tBLG_NUIsActive=1 and PortOfLoadName='" + Session["Loading"].ToString() + "' group by  PortOfDischargeName";
            }
            else if (Flag == "D")
            {
                sql = "Select PortOfLoadName from VW_TransAIHBLMasterUpdateDetails  where tBLG_NUIsActive=1 and PortOfDischargeName='" + Session["Destination"].ToString() + "' group by  PortOfLoadName";
            }
            else
            {
                sql = "";
            }

            SqlDataReader rdr = mobjGenlib.objDBLib.ExecuteQueryReader(sql.ToString());

            while (rdr.Read())
            {
                LocationName.Add(rdr.GetValue(0).ToString());
            }

            rdr.Close();

            for (i = 0; i < Convert.ToInt32(Session["Count"]); i += 1)
            {
                TextBox textBox = new TextBox();
                textBox.ID = "myTextBox" + i.ToString();
                textBox.Width = 200;
                textBox.Height = 15;
                //textBox.Text = String.Empty;
                textBox.Text = LocationName[i].ToString();
                textBox.BackColor = System.Drawing.Color.AliceBlue;
                myPlaceHolder.Controls.Add(textBox);
                dynamicTextBoxes[i] = textBox;

                var button = new Button();
                button.ID = "BtnAdd" + i.ToString();
                button.Text = "Add";
                //button.Click += button_Click;
                button.Click += new System.EventHandler(button_click);
               //btn.Click += new EventHandler(btn_Click);    
                myPlaceHolder.Controls.Add(button);
                dynamicButtons[i] = button;
                ViewState["Btn"] = dynamicButtons[i];

                LiteralControl literalBreak = new LiteralControl("<br />"); 
                myPlaceHolder.Controls.Add(literalBreak);

        gv.ID = "gridview" + i;
                myPlaceHolder.Controls.Add(gv);
                dynamicGV[i] = gv;
                BindGrid(textBox.Text, Flag, i);

                LiteralControl literalBreak1 = new LiteralControl("<br />");
                myPlaceHolder.Controls.Add(literalBreak1);
            }
}

This is for button click:

protected void button_click(object sender, EventArgs e)
{
            Button btn = (Button)sender;

            foreach (Button bt in dynamicButtons)
            {
                for (int i = 0; i < Convert.ToInt32(Session["Count"]); i++)
                {
                    if (btn.ID == ("BtnAdd" + i))
                    {
                        Response.Redirect("FrmTransLCLConsolidation.aspx");
                        break;
                    }
                }
            }
}

**

> i dont know how to write code to the Datagrid onitemcommand and > ondeletecommand.

**

Can anyone help me to achieve this? Thanks in advance.

enter image description here

another issue: enter image description here

aspx page:

 <asp:Repeater runat="server" OnItemDataBound="repeaterSearchResult_ItemDataBound" ID="repeaterSearchResult">
                                <ItemTemplate>
                                    <asp:Label ID="textBoxSearch"  ForeColor="White" width="25%" runat="server" Text="<%#Container.DataItem%>"></asp:Label>
                                    <%--<asp:TextBox ID="textBoxSearch" runat="server" Text="<%#Container.DataItem%>"></asp:TextBox>--%>
                                    <asp:Button ID="BTNAdd" runat="server" Text="Add"  OnClick="button_click"/><br />
                                    <asp:DataGrid ID="dgLCL" runat="server" BorderWidth="1px" BorderColor="#FE9B00"
                                                                    BorderStyle="Solid" BackColor="White" Font-Names="Verdana" Font-Size="XX-Small"
                                                                    AutoGenerateColumns="False" ShowFooter="FALSE" CellPadding="3" align="center"
                                                                    Width="700px" OnItemCommand="dgLCL_Select" OnDeleteCommand="dgLCL_Delete">
                                                                    <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
                                                                    <SelectedItemStyle Font-Bold="True" ForeColor="Black" BackColor="Snow"></SelectedItemStyle>
                                                                    <EditItemStyle BackColor="AntiqueWhite"></EditItemStyle>
                                                                    <PagerStyle BackColor="#FDE9CB" ForeColor="#003399" HorizontalAlign="Right" Mode="NumericPages"
                                                                    Position="Bottom" Font-Size="Small" Font-Bold="true" />
                                                                    <AlternatingItemStyle BackColor="Snow"></AlternatingItemStyle>
                                                                    <ItemStyle ForeColor="#000066" BackColor="Snow"></ItemStyle>
                                                                    <HeaderStyle Font-Size="XX-Small" Font-Bold="True" Height="10px" ForeColor="#000000"
                                                                    BackColor="#FFDBA6"></HeaderStyle>
                                                    <Columns>
                                                        <asp:TemplateColumn>
                                                        <ItemTemplate>
                                                                <asp:ImageButton runat="server" ID="IMGBTNAdd" ImageUrl="~/AppImages/grid-icon-add.jpg"
                                                                ToolTip="Insert"  CommandName="SelectItem" AlternateText="Insert" />

                                                        </ItemTemplate>
                                                         </asp:TemplateColumn>

                                                        <asp:BoundColumn DataField="MasterNo" HeaderText="MasterNo"></asp:BoundColumn>
                                                        <asp:BoundColumn DataField="MasterDate" HeaderText="MasterDate"></asp:BoundColumn>
                                                        <asp:BoundColumn DataField="GrossWt" HeaderText="GrossWt"></asp:BoundColumn>
                                                        <asp:BoundColumn DataField="GrossUOMType" HeaderText="Type"></asp:BoundColumn>
                                                        <asp:BoundColumn DataField="Volume" HeaderText="Volume"></asp:BoundColumn>
                                                        <asp:TemplateColumn HeaderText="DELETE">
                                                            <ItemTemplate>
                                                                <asp:ImageButton runat="server" ID="IMGBTNDelete" ImageUrl="~/AppImages/grid-icon-delete.jpg"
                                                                    ToolTip="Delete"  CommandName="DeleteItem" OnClientClick="javascript:return confirmDelete();"
                                                                    AlternateText="Delete" />
                                                            </ItemTemplate>
                                                        </asp:TemplateColumn>
                                                    </Columns>
                                                    <PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages">
                                                    </PagerStyle>
                                                </asp:DataGrid><br />
                                </ItemTemplate>

                        </asp:Repeater>
2
  • And the question is??? Commented Jun 30, 2014 at 7:31
  • how to create gridview and how to add button click event Commented Jun 30, 2014 at 7:39

1 Answer 1

1

Repeater is a good candidate for this problem. Instead of adding the textboxes dynamically use a repeater control here, which is then easier to maintain. Below is the outline of what you can do.

<asp:Repeater runat="server" OnItemDataBound="repeaterSearchResult_ItemDataBound" ID="repeaterSearchResult">
            <ItemTemplate>
                <asp:TextBox ID="textBoxSearch" runat="server" Text="<%#Container.DataItem%>"></asp:TextBox>
                <asp:Button ID="buttonAdd" runat="server" Text="Button"  OnClick="button_click"/>
                <asp:GridView ID="gridView" runat="server">
                    <%--Add the Columns you want to display--%>
                </asp:GridView>
            </ItemTemplate>

</asp:Repeater>

In your code, after getting the search result from the data reader, instead of creating the textbox and buttons, bind the result to the repeater

SqlDataReader rdr = mobjGenlib.objDBLib.ExecuteQueryReader(sql.ToString());
        while (rdr.Read())
        {
            LocationName.Add(rdr.GetValue(0).ToString());
        }
        rdr.Close();
        repeaterSearchResult.DataSource = LocationName;
        repeaterSearchResult.DataBind();

In the repeaters OnItemDataBound, bind the grid view

protected void repeaterSearchResult_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            GridView gv = e.Item.FindControl("gridView") as GridView;
            TextBox textBox = e.Item.FindControl("textBoxSearch") as TextBox;
            if (gv != null)
            {
                //Bind gridview here
            }
        }

In the button click event you can redirect to the page. You can also use the command argument for the button to identify the current index if you want.

Further to your question to open a new form you can use the following method. In your image button add onclientclick event as below.

<asp:ImageButton runat="server" OnClientClick="return openNewForm();" ID="IMGBTNAdd" ImageUrl="~/AppImages/grid-icon-add.jpg"
                                                                ToolTip="Insert"  CommandName="SelectItem" AlternateText="Insert" />

Then add a javascript function to open the popup

    <script type="text/javascript">
        function openNewForm() {
            window.open("url for the new form", "newForm", "menubar=0,resizable=1,location=0,status=0,scrollbars=1,height=500,width=600");
            return false;
        }
    </script>
Sign up to request clarification or add additional context in comments.

3 Comments

Hi @Kiran Hegde I need you precious time to solve my problem.please help me here is my que goes stackoverflow.com/questions/25262907/…
i need your help to solve my issue ..which i have mentioned in stackoverflow.com/questions/26018237/… can u please help me
hedge ...hi i am in a critical position can you please help me to find what is the issue in this stackoverflow.com/questions/28229164/…

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.