在gridview中编辑时,通过下拉列表的onchange事件更新文本框

时间:2009-11-12 08:02:53

标签: asp.net javascript gridview

我正在尝试在更改DropDownList上的项目时更新GridView中当前编辑的行中的TextBox,我无法在VB中完成它。我在c#中找到了这段代码,但不知道我是否走在正确的轨道上?

你能提供一些帮助吗?

PS:此代码适用于OnMouseOver事件,但重点是在编辑模式下更新TextBox。

<ItemTemplate>
    <asp:TextBox runat="server" ID="tx1" onmouseover='<%# "ChangeValue(" +((DataGridItem)Container).FindControl("tx1").ClientID + ")"%>'></asp:TextBox>
</ItemTemplate>

JS代码:

function ChangeValue(i)
{
    var t=i.id
    document.getElementById(t).value="Hello World!";
}

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

将以下脚本放在head标签中:

<script language="javascript" type="text/javascript">

    function ChangeValue(ddl,txtid)
    {
        var txt=document.getElementById(txtid);
        var dval= ddl.options[ddl.selectedIndex].value;
        if(dval=="1")
        {
         txt.value="It's 1";
        }
        if(dval=="2")
        {
         txt.value="It's 2";
        }
        if(dval=="3")
        {
         txt.value="It's 3";
        }

    }
    </script>

尝试以下网格视图:

 <asp:GridView ID="GridView1" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
                AutoGenerateColumns="false" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand"
                runat="server" OnRowCreated="GridView1_RowCreated">
                <Columns>
                    <asp:CommandField ButtonType="link" ShowEditButton="true" ShowCancelButton="true" />
                    <asp:TemplateField HeaderText="CategoryID">
                        <ItemTemplate>
                            <asp:LinkButton ID="lnkID" runat="server" CommandName="sel" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.CategoryID") %>'
                                Text='<%# DataBinder.Eval(Container,"DataItem.CategoryID") %>'></asp:LinkButton>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddl" runat="server">
                                <asp:ListItem Text="1" Value="1" Selected="true"></asp:ListItem>
                                <asp:ListItem Text="2" Value="2"></asp:ListItem>
                                <asp:ListItem Text="3" Value="3"></asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Comments">
                        <ItemTemplate>
                            <asp:Label ID="lblID" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.CategoryID") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtComments" runat="server"></asp:TextBox>

                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CategoryName">
                        <ItemTemplate>
                            <asp:LinkButton ID="lnkName" runat="server" CommandName="sel" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.CategoryName") %>'
                                Text='<%# DataBinder.Eval(Container,"DataItem.CategoryName") %>'></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

处理codebehind中的rowcreated事件:

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Edit)
        {
              DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
            TextBox txt = (TextBox)e.Row.FindControl("txtComments");
            txt.Text = "It's 1";
            //------------ Set onchange function for dropdown---------------------------//
            ddl.Attributes.Add("onchange", "javascript:ChangeValue(this,'" + txt.ClientID + "');");
        }
    }

使用northwind db中的Categories表绑定网格。