Asked by:
effect only select row in gridview

Question
-
User66371569 posted
<p>I am using this code</p>
<p>I have dropdwonlist inside griview I just want when I change drop downlist only selected row textbox get changed my code effects all rows </p>
<p></p>
<p>any help</p>
<p></p>
<p>Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)<br>
Dim chkChosen As DropDownList = CType(GridView4.SelectedRow.FindControl("DropDownList1"), DropDownList)<br>
Dim Coboneno As TextBox = GridView4.FindControl("Coboneno")<br>
If chkChosen.SelectedValue.ToString = "2" Then
Coboneno.Enabled = False<br>
ElseIf chkChosen.SelectedValue.ToString = "1" Then<br>
Coboneno.Enabled = True<br>
'
End If</p>
<p>End Sub</p>Thursday, September 27, 2018 11:14 AM
All replies
-
User-893317190 posted
Hi thepast,
I don't know how you design your gridview, but if you want to only effect selected row's textbox, I suggest you could put textbox in the itemtemplate and use GridView4.SelectedRow.FindControl.
Below is my code.
<asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="false" > <Columns> <asp:TemplateField> <ItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> </asp:DropDownList> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowSelectButton="true" /> </Columns> </asp:GridView>
Code behind.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then Dim ints As List(Of Int32) = New List(Of Integer) For index = 1 To 10 ints.Add(1) Next GridView1.DataSource = ints GridView1.DataBind() End If End Sub Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Dim chkChosen As DropDownList = CType(GridView1.SelectedRow.FindControl("DropDownList1"), DropDownList) Dim Coboneno As TextBox = CType(GridView1.SelectedRow.FindControl("TextBox1"), TextBox) If chkChosen.SelectedValue.ToString = "2" Then Coboneno.Enabled = False ElseIf chkChosen.SelectedValue.ToString = "1" Then Coboneno.Enabled = True ' End If End Sub
The result.
Best regards,
Ackerly Xu
Friday, September 28, 2018 2:08 AM -
User66371569 posted
Yes thank u thats what i want excatlly but i want when i change dropdownlist direct i dont want do use select on press i mean i want when dropdownlist changed
Can i do that?Friday, September 28, 2018 7:04 AM -
User-893317190 posted
Hi thepast,
You could do this through jquery.
Below is my code. GridView1 is the id of your gridview.
<script> $(function () { $("#<%=GridView1.ClientID %> select").change( function () { if ($(this).val() == "1") { $(this).siblings("input[type=text]").prop("disabled", false); } else { $(this).siblings("input[type=text]").prop("disabled", true); } } ) }) </script>
The you could remove the select button.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" > <Columns> <asp:TemplateField> <ItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnTextChanged="DropDownList1_SelectedIndexChanged" > <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> </asp:DropDownList> <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Best regards,
Ackerly Xu
Friday, September 28, 2018 8:20 AM -
User66371569 posted
Thank u so much for reply i eant to ask u if i have in this Gridview more than textbox and forexample
Textbox1
Textbox2
i want when i changed Dropdownlist only
Textbox1.text='' " 'clear first'
Textbox1.Enabled=False
And not touch Textbox2
How can i do it in functionFriday, September 28, 2018 8:31 AM -
User-893317190 posted
Hi thepast,
Still you could do it using jquery.
Below is my code.
":first" in "input[type=text]:first" means you want to choose the first textbox. Please pay attention of the order of you two textbox
If you don't want to disable the textbox just remove .prop("disabled", true).
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" > <Columns> <asp:TemplateField> <ItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnTextChanged="DropDownList1_SelectedIndexChanged" > <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> </asp:DropDownList> <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox> <!--this frst textbox will be disabled and cleared--> <asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <script> $(function () { $("#<%=GridView1.ClientID %> select").change( function () { if ($(this).val() == "1") { $(this).siblings("input[type=text]:first").prop("disabled", false); } else { $(this).siblings("input[type=text]:first").prop("disabled", true).val(""); } } ) }) </script>
Best regards,
Ackerly Xu
Friday, September 28, 2018 8:41 AM -
User66371569 posted
Acutally i have this Grideview
<asp:GridView ID="GridView4" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" style="font-weight: 700" Width="852px" PageIndex="5" Height="180px">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:TextBox ID="cobnumbers" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:TextBox ID="EndDate" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:TextBox ID="Startdate" runat="server" AutoPostBack="True" OnTextChanged="Startdate_TextChanged" Width="74px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="CoboneName" DataValueField="CoboneID" style="font-weight: 700" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:TextBox ID="Coboneno" runat="server" Height="21px" Width="60px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#94BE10" Font-Bold="True" Font-Size="12pt" ForeColor="White" />
<PagerStyle BorderColor="#8CAC21" BorderStyle="Solid" ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="White" BorderColor="#99CC00" Font-Size="11pt" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
First thing as i said i want when i change dropdowanlist
coboneno.text=' '
cobone.Enabled=False
Stratdate.text " same Up "
Enddate.text " same Up "
Second i want when start.text change event
Dim MyDate As Date = DateTime.ParseExact(Startdate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Dim DaysInMonth As Integer = Date.DaysInMonth(MyDate.Year, MyDate.Month)
Dim LastDayInMonthDate As Date = New Date(MyDate.Year, MyDate.Month, DaysInMonth)
EndDate.Text = LastDayInMonthDate.ToString("dd/MM/yyyy")
cobnumbers.Text = DateDiff(DateInterval.Day, DateTime.ParseExact(Startdate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture), DateTime.ParseExact(EndDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture))
I did that in normal page using textboxes only its working
How can i do it in grideview
Thank u so much
Friday, September 28, 2018 9:00 AM -
User-893317190 posted
Hi thepast,
You could try the code below, please pay attention to how I use the sender to get the dropdownlist and textbox and how I use the NamingContainer property
Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Dim dropdown = CType(sender, DropDownList) ' get the dropdown Dim startDate As TextBox = CType(dropdown.NamingContainer.FindControl("Startdate"), TextBox) Dim endDate As TextBox = CType(dropdown.NamingContainer.FindControl("EndDate"), TextBox) Dim coboneno As TextBox = CType(dropdown.NamingContainer.FindControl("Coboneno"), TextBox) If dropdown.SelectedValue = "1" Then startDate.Enabled = True endDate.Enabled = True coboneno.Enabled = True Else startDate.Enabled = False endDate.Enabled = False coboneno.Enabled = False startDate.Text = "" endDate.Text = "" coboneno.Text = "" End If End Sub Protected Sub Startdate_TextChanged(sender As Object, e As EventArgs) Dim startDate As TextBox = CType(sender, TextBox) ' get the startDate from sender Dim endDate As TextBox = CType(startDate.NamingContainer.FindControl("EndDate"), TextBox) Dim cobnumbers As TextBox = CType(startDate.NamingContainer.FindControl("cobnumbers"), TextBox) Dim MyDate As Date = DateTime.ParseExact(startDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture) Dim DaysInMonth As Integer = Date.DaysInMonth(MyDate.Year, MyDate.Month) Dim LastDayInMonthDate As Date = New Date(MyDate.Year, MyDate.Month, DaysInMonth) EndDate.Text = LastDayInMonthDate.ToString("dd/MM/yyyy") cobnumbers.Text = DateDiff(DateInterval.Day, DateTime.ParseExact(startDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture), DateTime.ParseExact(EndDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)) End Sub
Best regards,
Ackerly Xu
Friday, September 28, 2018 11:51 AM -
User66371569 posted
Thank u so so much
I have a quastion do i have to use also function with above code to effect row by row or just using only last code you sent enoughFriday, September 28, 2018 2:48 PM -
User66371569 posted
Hi there
I tested your last code
when select changed all rows in gridview get effect
second code when I change I write startdate nothing happened startdate gets clear and also effect all rows in gridview
Thank you ….
Saturday, September 29, 2018 6:04 AM -
User-893317190 posted
Hi thepast,
You only need to write the code I last posted and you don't need to write js.
In my case , when I select 2 in dropdownlist, only the textbox in the the row get effected.
When I choose 1 and text in startdate textbox, only the textbox in the same row get effected.
What do you mean by nothing happened? The OnTextChanged event will only trigger when the textbox is not focused,so when you only change the text ,it will not happen immediately.Please click outside the textbox after you change the textbox.
Best regards,
Ackerly Xu
Tuesday, October 2, 2018 1:01 AM