簡體   English   中英

如何在GridViewRow編輯模式下找到TextBox

[英]How to find TextBox in GridViewRow edit mode

我為此嘗試了幾個所謂的答案,這讓我迷失了方向。 我只是試圖使用今天的日期和時間默認一個TextBox Text值,但是當我用CommandName“ Edit”單擊LinkButton時找不到LinkButton

這是我的gridview ...

   <asp:GridView ID="gvSignInRegister" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" 
        DataSourceID="sdsSignInRegister" ForeColor="Black" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" GridLines="Vertical" OnRowCommand="gvSignInRegister_RowCommand1">
        <Columns>
            <asp:TemplateField HeaderText="Returned" SortExpression="DateTimeReturned">
                <EditItemTemplate>
                    <asp:TextBox ID="txtReturned" runat="server"></asp:TextBox>
                    <asp:ImageButton runat="Server" ID="calImg" ImageUrl="~/images/Calendar_scheduleHS.png" AlternateText="Click to show calendar" CausesValidation="False" />
                    <asp:RequiredFieldValidator ID="rfv1" runat="server" SetFocusOnError="true" ValidationGroup="vg1" ControlToValidate="txtReturned" ErrorMessage="Required"></asp:RequiredFieldValidator>
                    <ajaxToolkit:CalendarExtender ID="ce1" runat="server" PopupButtonID="calImg" Enabled="true" Format="dd/MM/yyyy" TargetControlID="txtReturned" PopupPosition="TopRight" OnClientDateSelectionChanged="AppendTime"></ajaxToolkit:CalendarExtender>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label9" runat="server" Text='<%# Eval("DateTimeReturned","{0:dd/MM/yyyy HH:mm}") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField ShowHeader="False">
                <EditItemTemplate>
                    <asp:Button ID="btnCAN" runat="server" CausesValidation="false" CommandName="Cancel" Text="CANCEL" />
                    <asp:Button ID="btnUPD" runat="server" ValidationGroup="vg1" CausesValidation="true" CommandName="Update" Text="UPDATE" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Button ID="btnEDT" runat="server" CausesValidation="false" CommandName="Edit" CommandArgument='<%# Container.DataItemIndex %>' Text="SIGN IN" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#CCCCCC" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="#CCCCCC" />
    </asp:GridView>

LinkBut​​ton btnEDT起作用,並將gridview置於編輯模式。 但是在后面的代碼中我找不到“ txtReturned”。

到目前為止,這是我嘗試過的...

protected void gvSignInRegister_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Edit")
    {
        int rowIdx = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = gvSignInRegister.Rows[rowIdx];
        if (row != null && row.RowType == DataControlRowType.DataRow)
        {
            TextBox tb = (TextBox)row.FindControl("txtReturned");
            if (tb != null) tb.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm");

            //I've tried this too but it does not work. Interestingly, it does not crash, so cells[4] must exist!
            //row.Cells[4].Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
        }
    }
}

由於某些原因,rowIdx始終為0。為什么? 我認為行索引為0意味着gridview控件的標題。

我也嘗試過使用大多數其他人在其他帖子中建議的NamingContainer,但這會返回一個空白(我懷疑是新的?)GridViewRow。

GridViewRow row =(GridViewRow)((GridViewRow)(e.CommandSource).NamingContainer);

UPDATE

我發現了這個 ,這正是我遇到的問題,但是通過RowEditing的解決方案仍然找不到文本框!

但是RowDataBound()解決了它! 請在下面閱讀我的答案。

使用Container.DisplayIndex而不是Container.DataItemIndex

但是我不認為如果將其放置在EditItemTemplate中,您將獲得textBox控件

如果您期望編輯操作,請使用以下代碼

HTML

<asp:GridView ID="gvSignInRegister" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3"           ForeColor="Black" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" OnRowEditing="gvSignInRegister_RowEditing" OnRowCancelingEdit="gvSignInRegister_RowCancelingEdit"  OnRowUpdating ="gvSignInRegister_RowUpdating" GridLines="Vertical">
            <Columns>
                <asp:TemplateField HeaderText="Returned" SortExpression="DateTimeReturned">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtReturned" Text='<%#Bind("DateTimeReturned", "{0:dd/MM/yyyy HH:mm}")%>'  runat="server"></asp:TextBox>
                        <asp:ImageButton runat="Server" ID="calImg" ImageUrl="~/images/Calendar_scheduleHS.png" AlternateText="Click to show calendar" CausesValidation="False" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label9" runat="server" Text='<%# Eval( "DateTimeReturned","{0:dd/MM/yyyy HH:mm}") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="true" />
            </Columns>
            <FooterStyle BackColor="#CCCCCC" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="#CCCCCC" />

        </asp:GridView>

背后的代碼:

    protected void gvSignInRegister_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvSignInRegister.EditIndex = e.NewEditIndex;
        List<QuotationDetail> itemList = (List<QuotationDetail>)ViewState["ItemList"];
        gvSignInRegister.DataSource = itemList;
        gvSignInRegister.DataBind();
    }

    protected void gvSignInRegister_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

    }

    protected void gvSignInRegister_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        var txtQty = (TextBox)gvSignInRegister.Rows[e.RowIndex].FindControl("txtQuantity");
        decimal qty = 0;
        decimal.TryParse(txtQty.Text, out qty);

        if (qty < 0)
        {
            lblErrorSummary.InnerText = "Please provide valid Quantity";
            lblErrorSummary.Visible = true;
            return;
        }
        itemList[e.RowIndex].Quantity = qty
        ViewState["ItemList"] = itemList;
        gvSignInRegister.EditIndex = -1;
        gvSignInRegister.DataSource = itemList;
        gvSignInRegister.DataBind();
    }

答案是進入GridView本身的editmode版本,然后找到控件!

根據這篇文章 ...

<asp:GridView ID="gvSignInRegister" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" 
            DataSourceID="sdsSignInRegister" ForeColor="Black" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" GridLines="Vertical" OnRowDataBound="gvSignInRegister_RowDataBound">
            <Columns> ...etc...


protected void gvSignInRegister_RowDataBound(object sender, GridViewRowEventArgs e)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                        {
                            TextBox tb = (TextBox)e.Row.FindControl("txtReturned");
                            if (tb != null) tb.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
                        }
                    }
                }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM