开发者

Update textbox via onchange event of a dropdownlist while editing in gridview

I am trying to update a TextBox within currently edited row within a GridView on when changing items on a DropDownList and I cant quite get it going in VB. I found this code in c# but don't know if I'm on the right track?

Can you please offer some help?

开发者_如何学Go

PS: This code is for an OnMouseOver event but the point is to update the TextBox while in edit mode.

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

JS Code:

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


Try the following:

Put the following script in head tag:

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

Try the gridview below:

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

Handle the rowcreated event in codebehind:

 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 + "');");
        }
    }

Bind the grid with Categories table in northwind db.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜