Assign Selected Text Value Of Dropdown List To Textbox In Another Cell Of Same Row

Assigning selected text of dropdown in textbox of GridView

If you want to assign selected text value of dropdown list to textbox that is present in another cell of the same row, you can use jQuery to achieve the same. Kindly refer to  the below example for the same.

Add GridView to ASPX page
  1. <asp:GridView ID="grvCurrency" runat="server" CssClass="tablesaw table table-condensed table-striped table-bordered tablesaw-swipe hdt" AutoGenerateColumns="False" OnRowDataBound="grvCurrency_OnRowDataBound">  
  2.     <Columns>  
  3.         <asp:TemplateField HeaderText="Currency">  
  4.             <ItemTemplate>  
  5.                 <asp:Label ID="lblProjectCurrency" runat="server" Text='<%# Bind("ProjectCurrencyID")%>' Visible="false"></asp:Label>  
  6.                 <asp:Label ID="lblCurrency" runat="server" Text='<%# Bind("CurrencyID")%>' Visible="false"></asp:Label>  
  7.                 <asp:DropDownList ID="ddlCurrency" CssClass="form-control ddlclass" runat="server"> </asp:DropDownList>  
  8.             </ItemTemplate>  
  9.         </asp:TemplateField>  
  10.         <asp:TemplateField HeaderText="Name">  
  11.             <ItemTemplate>  
  12.                 <asp:TextBox ID="txtProjectCurrencyName" MaxLength="100" Text='<%# Bind("ProjectCurrencyName") %>' CssClass="form-control" runat="server"> </asp:TextBox>  
  13.             </ItemTemplate>  
  14.         </asp:TemplateField>  
  15.         <asp:TemplateField HeaderText="Value">  
  16.             <ItemTemplate>  
  17.                 <asp:TextBox ID="txtCurrencyValue" MaxLength="100" Text='<%# Bind("ProjectCurrencyValue") %>' CssClass="form-control" runat="server"> </asp:TextBox>  
  18.             </ItemTemplate>  
  19.         </asp:TemplateField>  
  20.         <asp:TemplateField HeaderText="Action" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" HeaderStyle-Width="5%">  
  21.             <ItemTemplate>  
  22.                 <asp:LinkButton ID="lnkAdd" runat="server" CommandArgument='<%# Eval("ProjectCurrencyID") %>' ToolTip="Add" Text="Add" OnClick="lnkAdd_OnClick"> <span class="fa fa-plus"></span> </asp:LinkButton>  
  23.                 <asp:LinkButton ID="lnkDelete" runat="server" CommandArgument='<%# Eval("ProjectCurrencyID") %>' ToolTip="Delete" OnClientClick="return DeleteConfirmation()" Text="Delete" OnClick="lnkDelete_OnClick"> <span class="fa fa-trash"></span> </asp:LinkButton>  
  24.             </ItemTemplate>  
  25.         </asp:TemplateField>  
  26.     </Columns>  
  27. </asp:GridView>  
Now, bind the currency with data.
  1. private void BindProjectCurrencies(DataTable dtCurrencies) {  
  2.         if (dtCurrencies != null && dtCurrencies.Rows.Count > 0) {  
  3.             List < ProjectCurrency > projectCurrency = new List < ProjectCurrency > ();  
  4.             foreach(DataRow dr in dtCurrencies.Rows) {  
  5.                 projectCurrency.Add(new ProjectCurrency() {  
  6.                     ProjectCurrencyID = Convert.ToInt32(dr[FIELD_PROJECT_CURRENCY_ID]),  
  7.                         CurrencyID = Convert.ToInt32(dr[FIELD_CURRENCY_ID]),  
  8.                         ProjectID = Convert.ToInt32(dr[FIELD_PROJECT_ID]),  
  9.                         ProjectCurrencyName = dr[FIELD_PROJECT_CURRENCY_NAME].ToString(),  
  10.                         CreatedBy = Convert.ToInt32(dr[FIELD_CREATED_BY]),  
  11.                         CreatedOn = Convert.ToDateTime(dr[FIELD_CREATED_ON]),  
  12.                         ModifiedBy = Convert.ToInt32(dr[FIELD_MODIFIED_BY]),  
  13.                         ModifiedOn = Convert.ToDateTime(dr[FIELD_MODIFIED_ON]),  
  14.                         ProjectCurrencyValue = dr[FIELD_PROJECT_CURRENCY_VALUE].ToString(),  
  15.                         IsActive = Convert.ToBoolean(dr[FIELD_IS_ACTIVE])  
  16.                 });  
  17.             }  
  18.             projectCurrency.Insert(0, new ProjectCurrency());  
  19.             grvCurrency.DataSource = projectCurrency;  
  20.             grvCurrency.DataBind();  
  21.         }  
 Add the following script for the same.
  1. $(function() {  
  2.     $("[id*=ddlCurrency]").change(function() {  
  3.         var ddy = $(this).find('option').filter(':selected').text();  
  4.         // ddy = ddy.substr(ddy.indexOf("-") + 1);  
  5.         var row = $(this).closest("tr");  
  6.         $("[id*=txtProjectCurrencyName]", row).val(ddy);  
  7.     });  
  8. });  
Here, we are triggering change event of controls which are having name ending with ddlCurrency

i.e.
  1. $("[id*=ddlCurrency]")  
.change

Afte this, we are finding the selected value of dropdown list i.e.
  1. var ddy = $(this).find('option').filter(':selected').text();  
Now, we are trying to get closest tr of that control.

After this, we are fetching control whose name is ending with txtProjectCurrencyName of that row

i.e.
  1. $("[id*=txtProjectCurrencyName]",row)  
And finally, we are assigning value to the textbox i.e. $
  1. ("[id*=txtProjectCurrencyName]",row).val(ddy);  
Now, we want to validate values of controls of a row on click of button

In the above Grid, we have taken link button as lnkAdd.

Now, in script, use the below code.
  1. $(function() {  
  2.     $("[id*=grvCurrency] [id*=lnkAdd]").click(function() {  
  3.         //Find the GridView Row using the LinkButton reference.  
  4.         var row = $(this).closest("tr");  
  5.         //Find the TextBox control.  
  6.         var txtPrjCurrencyName = row.find("[id*=txtProjectCurrencyName]");  
  7.         var txtCurrencyValue = row.find("[id*=txtCurrencyValue]");  
  8.         var ddlCurrency = row.find("[id*=ddlCurrency]");  
  9.         var message = "";  
  10.         if (ddlCurrency.val().trim() === "---Select Currency ---") {  
  11.             message += "Please select Currency.";  
  12.         }  
  13.         if ($.trim(txtPrjCurrencyName.val().trim()) === "") {  
  14.             message += "Please enter Name.\n";  
  15.         }  
  16.         if ($.trim(txtCurrencyValue.val().trim()) === "" || $.trim(txtCurrencyValue.val()) === '0') {  
  17.             message += "Please enter value.\n";  
  18.         }  
  19.         //Display error message.  
  20.         if (message !== "") {  
  21.             alert(message);  
  22.             return false;  
  23.         }  
  24.         return true;  
  25.     });  
  26. });  
Now, here we are triggering click event of controls which are having names ending with lnkAdd i.e.
  1. $("[id*= lnkAdd]")  
After this, we are fetching closet row of the control.
 
click i.e. $(this).closest("tr");

Now, we are trying to fetch controls present in that row by their name.
  1. row.find("[id*=txtProjectCurrencyName]");  
And after that, we are putting validation for specific control.
  1. if (ddlCurrency.val().trim() === "---Select Currency ---")
  2. {  
  3.     message += "Please select Currency.";  
  4. }  
Ebook Download
View all
Learn
View all