views:

800

answers:

2

Hi All,

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 dont know if Im on the right track?

Can you please offer some help?

Thanks for your time.

PS this code is for an onmouseover event but the point is to update the textbox while in edit mode.

'>

Js:

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

I think should be like

document.getElementById('<%= TextBoxUnitPrice.ClientID %>').value = "Hello World!";
Muhammad Akhtar
txt does not exist, this causes a js error. Its only when you are editing within the gridview mode that you can access it.
Matt S
Allow me to elaborate, var txt document.getElementById('<%=TextBoxUnitPrice.ClientID %>'); causes a js error :Name 'TextBoxUnitPrice' is not declaredBecause Im guessing that it is not visible until the gridview is in edit mode, so how do I pass the textbox ID to be updated when the selected index changes of a dropdownlist?
Matt S
A: 

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.

Himadri
Thats it alright. I was walking down the wrong path, I just got to this stage.Thanks for your time and understanding...
Matt S
Protected Sub DataGrid_InvoiceItems_RowEditing(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles DataGrid_InvoiceItem.RowEditing DataGrid_InvoiceItem.EditIndex = e.NewEditIndex BindInvoiceItems() LoadInvoice()
Matt S
Dim ddlProduct As DropDownList = CType(DataGrid_InvoiceItem.Rows(e.NewEditIndex).FindControl("ddlProduct"), DropDownList) Dim TextBoxUnitPrice As TextBox = CType(DataGrid_InvoiceItem.Rows(e.NewEditIndex).FindControl("TextBoxUnitPrice"), TextBox) If ddlProduct IsNot Nothing AndAlso TextBoxUnitPrice IsNot Nothing Then ddlProduct.Attributes.Add("onchange", "showValue('" + TextBoxUnitPrice.ClientID + "');") End If End Sub
Matt S
Thanks HimadriThis was definatelty the killer line...in the rowcreated eventddl.Attributes.Add("onchange", "javascript:ChangeValue(this,'" + txt.ClientID + "');");
Matt S