无法在GridViewRowCommand事件中更新TextBox的文本 [英] Can't update the text of a TextBox in the GridViewRowCommand Event

查看:74
本文介绍了无法在GridViewRowCommand事件中更新TextBox的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Gridview和一个文本框,它们放置在 Ajax updatepanel 中.

来源如下:

Hi I''ve a Gridview and a textbox which are placed in an Ajax updatepanel.

The source is given below:

 <asp:UpdatePanel ID="UpdatePanelAdmissionDefaultSettings" runat="server">
    <ContentTemplate>
<asp:GridView ID="GridViewMeritDetails" runat="server" AutoGenerateColumns="False" 

            BackColor="White" BorderColor="#5C5C5C" BorderStyle="Solid" BorderWidth="1px" 

            CellPadding="4" ForeColor="Black" CssClass="MeritGridStyle" 

                onrowdatabound="GridViewMeritDetails_RowDataBound" 

                onrowcommand="GridViewMeritDetails_RowCommand">
            <RowStyle BackColor="#F7F7DE" />
            <Columns>
                <asp:BoundField HeaderText="Field Name" DataField="MeritField" />
                <asp:BoundField HeaderText="Field ID" DataField="MeritFieldID" />
                <asp:ButtonField CommandName="edit" HeaderText="Edit" Text="Edit" />
                <asp:ButtonField CommandName="delete" HeaderText="Delete" Text="Delete" />
            </Columns>
            <FooterStyle BackColor="#CCCC99" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>

<asp:TextBox ID="TextBoxMeritFieldName" runat="server"></asp:TextBox>
</ContentTemplate>
    </asp:UpdatePanel>



我的问题是,当我单击GridView中的编辑"按钮时,我想在文本框中所选行的单元格中显示该值.我已经给出了将其显示在Gridview RowCommand事件中的代码.但这不起作用.

这是背后的代码:



My problem is, when I click edit button in GridView, I want to display the value in a cell in the selected row in the textbox. I''ve given the code to display it in Gridview RowCommand event. But it''s not working.

Here is the code behind:

protected void GridViewMeritDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int iRowIndex = Convert.ToInt32(e.CommandArgument);
        if (e.CommandName == "edit")
        {
            TextBoxMeritFieldName.Enabled = true;
            TextBoxMeritFieldName.Text = GridViewMeritDetails.Rows[iRowIndex].Cells[0].Text.Trim();
        }
        else if (e.CommandName == "delete")
        {
            //Add code to delete merit field
        }
    }



当我调试时,控件进入RowCommand并到达该行



When I debugged, the control enters the RowCommand and reaches the line

TextBoxMeritFieldName.Text = GridViewMeritDetails.Rows[iRowIndex].Cells[0].Text.Trim();



但是文本不会显示在页面上.我缺少什么?



But the text is not displayed on the page. What am I missing?

推荐答案

将触发器添加到更新面板.此
Add the triggers to your update panel.This Link will be useful for you.
After that also it is not working means put your textbox in a seperate updatepanel and add the triggers and set the event.


您好,我通过将代码修改为以下内容来解决了这个问题:
Hi I solved the problem by modifying the code to the following:
<asp:updatepanel id="UpdatePanelAdmissionDefaultSettings" runat="server" xmlns:asp="#unknown">
    <contenttemplate>
<asp:gridview id="GridViewMeritDetails" runat="server" autogeneratecolumns="False">
            BackColor="White" BorderColor="#5C5C5C" BorderStyle="Solid" BorderWidth="1px" 
            CellPadding="4" ForeColor="Black" CssClass="MeritGridStyle" 
                onrowdatabound="GridViewMeritDetails_RowDataBound" 
                onrowcommand="GridViewMeritDetails_RowCommand">
            <rowstyle backcolor="#F7F7DE" />
            <columns>
                <asp:boundfield headertext="Field Name" datafield="MeritField" />
                <asp:boundfield headertext="Field ID" datafield="MeritFieldID" />
                <asp:buttonfield commandname="edit" headertext="Edit" text="Edit" />
                <asp:buttonfield commandname="delete" headertext="Delete" text="Delete" />
            </columns>
            <footerstyle backcolor="#CCCC99" />
            <pagerstyle backcolor="#F7F7DE" forecolor="Black" horizontalalign="Right" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <alternatingrowstyle backcolor="White" />
        </asp:gridview>
 
<asp:textbox id="TextBoxMeritFieldName" runat="server"></asp:textbox>
</contenttemplate>
<triggers>
<asp:asyncpostbacktrigger controlid="GridViewMeritDetails" />
</triggers>
    </asp:updatepanel>



我所做的更改是添加以下代码:



The change I made is,adding the following code:

<triggers>
    <asp:asyncpostbacktrigger controlid="GridViewMeritDetails" />
</triggers>



注意:

如果 EventName 属性添加到
,则该代码将不起作用



Note:

The code won''t work if EventName attribute is added to

<asp:asyncpostbacktrigger controlid="GridViewMeritDetails" />





改进的答案:



我对触发器进行了进一步的研究,发现了为什么这段代码有效. AsyncPostbackTrigger的EventName属性是可选的.如果未指定,则使用Control的DefaultEventAttribute来确定默认事件.

引用 [





Improved answer:



I did further study on the Triggers and found out why this code worked. The EventName property of AsyncPostbackTrigger is optional. If not specified, the DefaultEventAttribute of The control is used to determine the default event.

Refer[^]


这篇关于无法在GridViewRowCommand事件中更新TextBox的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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