Click here to Skip to main content
15,906,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I Cant find the textbox id in the code behind.I have tried following things:
1.Commented and added new text box with different id.
2.Deleted the Edit Item template and added the new template.
3.Copied the entire Grid and pasted in a new web form[Here all the textbox are not available in the back ground]
4. Build,Rebuild,Clean the solution.
The code works properly.
ASP.NET
<asp:GridView ID="grdDailyReport" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
            BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
            ShowFooter="True" CellPadding="3" CellSpacing="2" OnRowCommand="grdDailyReport_RowCommand">
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <Columns>
                <asp:TemplateField>
                    <FooterTemplate>
                        <asp:LinkButton ID="lnkDelete" runat="server">Delete</asp:LinkButton>
                    </FooterTemplate>
                    <HeaderTemplate>
                        <asp:CheckBox ID="chkDeleteAll" runat="server" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkDelete" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Id" InsertVisible="False" SortExpression="Id">
                    <EditItemTemplate>
                        <asp:Label ID="lblID" runat="server" Text='<%# Eval("Id") %>'>
                        </asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblID2" runat="server" Text='<%# Bind("Id") %>'>
                        </asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="WithdrawalAmount">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtWithdrawalAmount" runat="server" Text='<%# Bind("WithdrawalAmount") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblWithdrawalAmount" runat="server" Text='<%# Eval("WithdrawalAmount") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Balance">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtBalance" runat="server" Text='<%# Bind("Balance") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblBalance" runat="server" Text='<%# Bind("Balance") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="BankName">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlBankName" runat="server" SelectedValue='<%# Bind("BankName") %>'>
                            <asp:ListItem>--Select--</asp:ListItem>
                            <asp:ListItem>ICICI</asp:ListItem>
                            <asp:ListItem>IDBI</asp:ListItem>
                            <asp:ListItem>SBI</asp:ListItem>
                            <asp:ListItem>Axis</asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblBankName" runat="server" Text='<%# Bind("BankName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="WorkerLabour">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtWorkerLabour" runat="server" Text='<%# Bind("WorkerLabour") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblWorkerLabour" runat="server" Text='<%# Bind("WorkerLabour") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="PurchaseMatrialAmount">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtPurchaseMatrialAmount" runat="server" Text='<%# Bind("PurchaseMatrialAmount") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblPurchaseMatrialAmount" runat="server" Text='<%# Bind("PurchaseMatrialAmount") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="PurchaseAgainst">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlPurchaseAgainst" runat="server" SelectedValue='<%# Bind("PurchaseAgainst") %>'>
                     <asp:ListItem>--Select--</asp:ListItem>
                    <asp:ListItem>Furniture</asp:ListItem>
                    <asp:ListItem>Flooring</asp:ListItem>
                    <asp:ListItem>AC</asp:ListItem>
                    <asp:ListItem>Maintenance</asp:ListItem> 
                    <asp:ListItem>Water</asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblPurchaseAgainst" runat="server" 
                        Text='<%# Bind("PurchaseAgainst") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Comments">
               <EditItemTemplate>
                   <asp:TextBox ID="txtComments" runat="server" Text='<%# Bind("Comments") %>'></asp:TextBox>
               </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblComments" runat="server" Text='<%# Bind("Comments") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <EditItemTemplate>
                    <asp:LinkButton ID="lnkUpdate" runat="server" CommandArgument='<%# Eval("Id") %>'  CommandName="UpdateRow">Update</asp:LinkButton>
                    <asp:LinkButton ID="lnkCancel" runat="server" CommandArgument='<%# Eval("Id") %>' CommandName="CancelRow">Cancel</asp:LinkButton>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:LinkButton ID="lnkEdit" runat="server" CommandArgument='<%# Eval("Id") %>'  CommandName="EditRow">Edit</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
    </asp:GridView>


Want the control in the button click
C#
protected void btnsubmit_Click(object sender, EventArgs e)
{
    int WithdrawalAmount = Convert.ToInt16(txtWithdrawalAmount.Text.Trim());
    int Balance = Convert.ToInt16(txtBalance.Text.Trim());
    string BankName = ddlBankName.SelectedValue;
    string WorkerLabour = txtWorkerLabour.Text.Trim();
    int PurchaseMatrialAmount =Convert.ToInt16(txtlPurchaseMatrialAmount.Text.Trim());
    string PurchaseAgainst = ddlPurchaseAgainst.SelectedValue;
    //string Comments = txtComments.Text.Trim();
        
 //DataAccessLayer.SaveDailyReport(WithdrawalAmount,Balance,BankName,WorkerLabour,PurchaseMatrialAmount,PurchaseAgainst,Comments);
    BindData();
}
Posted
v3

Hi
You cannot find the control which is present inside a grid view in a normal way. the below code you need to write to find the text box control under a data source control.
when u define a control at page level then u can achive this in the code behind normally but in case of grid view you need to write the following code.

TextBox txtQtyval1 = (TextBox)gvExcParts.Rows[i].Cells[1].FindControl("txtRMAPartQuantity");

Finding a control inside a gridview row[^]

Hope this helps
Vamsi krisna
 
Share this answer
 
Comments
Member 9176543 4-Nov-13 4:00am    
Hi,
What is Rows[i],i cant write i it says its not found.I tried
string Comments = Convert.ToString((TextBox)grdDailyReport.Rows[0].Cells[8].FindControl("txtComments"));
but the value in the comment variable is blank.
And how other fields are allowed except this Comment 1.
vamforvamsi 4-Nov-13 6:42am    
its basically an indexer in the for loop.
for(int i=0;i
You can't do like this as there are TextBoxes for each Row.

For instance txtWithdrawalAmount will be present in all rows of the GridView.
So, if you access like below...
C#
int WithdrawalAmount = Convert.ToInt16(txtWithdrawalAmount.Text.Trim());

It will not work.

Rather you should run a Loop foreach Row of GridView and inside the Loop get the TextBox values.
 
Share this answer
 
Comments
Member 9176543 4-Nov-13 5:43am    
Hi Tadit Dash,
The other controls works fine like if i hard code the txtComments field then all other controls works properly i.e i can perform CRUD operations
Here is my DataAccessLayer
public static int SaveDailyReport(int WithdrawalAmount, int Balance, string BankName, string WorkerLabour, int PurchaseMatrialAmount, string PurchaseAgainst, string Comments)
{
DataSet ds = new DataSet();
string CS = ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand command = new SqlCommand("usp_sel_DailyReport", con);
command.CommandType = CommandType.StoredProcedure;
SqlParameter ParamWithdrawalAmount = new SqlParameter("@WithdrawalAmount", WithdrawalAmount);
command.Parameters.Add(ParamWithdrawalAmount);
SqlParameter ParamBalance = new SqlParameter("@Balance", Balance);
command.Parameters.Add(ParamBalance);
SqlParameter ParamBankName = new SqlParameter("@BankName", BankName);
command.Parameters.Add(ParamBankName);
SqlParameter ParamWorkerLabour = new SqlParameter("@WorkerLabour", WorkerLabour);
command.Parameters.Add(ParamWorkerLabour);
SqlParameter ParamPurchaseMatrialAmount = new SqlParameter("@PurchaseMatrialAmount", PurchaseMatrialAmount);
command.Parameters.Add(ParamPurchaseMatrialAmount);
SqlParameter ParamPurchaseAgainst = new SqlParameter("@PurchaseAgainst", PurchaseAgainst);
command.Parameters.Add(ParamPurchaseAgainst);
SqlParameter ParamComments = new SqlParameter("@Comments", Comments);
command.Parameters.Add(ParamComments);
con.Open();
return command.ExecuteNonQuery();


}
}
Member 9176543 4-Nov-13 6:31am    
Hi Tadit Dash,
I tried to loop it but it doesnt works. It throughs an exception
protected void btnsubmit_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdDailyReport.Rows)
{
int WithdrawalAmount = Convert.ToInt16(((TextBox)row.FindControl("txtWithdrawalAmount")).Text);
int Balance = Convert.ToInt16(((TextBox)row.FindControl("txtBalance")).Text);
string BankName = ((DropDownList)row.FindControl("ddlBankName")).SelectedItem.Value;
string WorkerLabour = ((TextBox)row.FindControl("txtWorkerLabour")).Text;
int PurchaseMatrialAmount = Convert.ToInt16(((TextBox)row.FindControl("txtlPurchaseMatrialAmount")).Text);
string PurchaseAgainst = ((DropDownList)row.FindControl("ddlPurchaseAgainst")).SelectedItem.Value;
string Comments = ((TextBox)row.FindControl("txtComments")).Text;
DataAccessLayer.SaveDailyReport(WithdrawalAmount, Balance, BankName, WorkerLabour, PurchaseMatrialAmount, PurchaseAgainst, Comments);

}
BindData();
What is the Exception?
Member 9176543 5-Nov-13 9:59am    
Object reference not set to an instance of an object.
on the First Line.
I have verified all the id names.
Okay let's try this. I am commenting all the codes and we will first try for the first TextBox. Try the below code.

foreach (GridViewRow row in grdDailyReport.Rows)
{
int WithdrawalAmount = 0;

TextBox textBoxWithdrawalAmount = (TextBox)row.FindControl("txtWithdrawalAmount");

int.TryParse(textBoxWithdrawalAmount.Text, out WithdrawalAmount);

// int Balance = Convert.ToInt16(((TextBox)row.FindControl("txtBalance")).Text);
// string BankName = ((DropDownList)row.FindControl("ddlBankName")).SelectedItem.Value;
// string WorkerLabour = ((TextBox)row.FindControl("txtWorkerLabour")).Text;
// int PurchaseMatrialAmount = Convert.ToInt16(((TextBox)row.FindControl("txtlPurchaseMatrialAmount")).Text);
// string PurchaseAgainst = ((DropDownList)row.FindControl("ddlPurchaseAgainst")).SelectedItem.Value;
// string Comments = ((TextBox)row.FindControl("txtComments")).Text;
// DataAccessLayer.SaveDailyReport(WithdrawalAmount, Balance, BankName, WorkerLabour, PurchaseMatrialAmount, PurchaseAgainst, Comments);
}

Now, debug and check if the value of WithdrawalAmount is coming correct or not for the first row after executing int.TryParse() line.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900