无法从代码后面访问文本框 [英] Cant Access Textbox from code behind

查看:44
本文介绍了无法从代码后面访问文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我无法在后面的代码中找到文本框ID。我尝试了以下内容:

1.评论并添加了具有不同ID的新文本框。

2.删除编辑项目模板并添加新模板。

3.复制整个网格并粘贴到新的网页表单[此处所有文本框都不可用在后台]

4.构建,重建,清理解决方案。

代码正常工作。

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: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



Want the control in the button click

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();
}

推荐答案

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
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


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 acc ess like below...

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...
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.


It will not work.

Rather you should run a Loop foreach Row of GridView and inside the Loop get the TextBox values.


这篇关于无法从代码后面访问文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆