Solved

How to get other control value at grid view?

Posted on 2007-12-05
10
221 Views
Last Modified: 2008-02-01
Hi,

I have a gridview which have several controls which is bind to a datatable.

The grid have dropdownlist and textbox. When the textbox is fill with something then on textchanged I want to know what is the dropdownlist selected value.

Please help.

Thank you.







0
Comment
Question by:emi_sastra
  • 5
  • 3
  • 2
10 Comments
 
LVL 11

Expert Comment

by:Robb Hill
ID: 20414403
My first question is what is the order of events on the page then I can give you a solution.  This is what I am assuming, please let me know if my assumptions are correct.

Your gridview loads.  You have ddlist and tbox on every line in gview.  a user types on a row a value in a text box.  After typing and leaving the text box you want to know the selected value of the dropdown list in that row of the gridview?
0
 
LVL 11

Expert Comment

by:Robb Hill
ID: 20414459
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)

        Dim index As Integer = GridView1.EditIndex
        Dim row As GridViewRow = GridView1.Rows(index)
       
        Dim dropdownlistobjectvariableAM As DropDownList = CType(row.FindControl("YourDropDownList"), DropDownList)
     
        e.NewValues("your_field") = dropdownlistobjectvariableAM.SelectedValue
0
 
LVL 16

Accepted Solution

by:
GreymanMSC earned 500 total points
ID: 20416435
Make sure the textbox's AutoPostBack property is set to "True".   This makes the TextChanged event fire.

Then in the TextChanged event, find the dropdown list in the textbox's parent's controls collection.  This will locate the dropdownlist in the same row of the gridview.
Protected Sub TheTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    '<!-- locate the drop down list relative to the sender -->
    Dim T As TextBox = CType(sender, TextBox)
    Dim D As DropDownList = T.Parent.FindControl("TheDropDownList")
    If Not D Is Nothing Then
        '<!-- do stuff with it... -->
        Dim Okay As Boolean = False
        For Each Li As ListItem In D.Items
            If Li.Text.Trim.Equals(T.Text.Trim) Then
                D.SelectedValue = Li.Value
                T.Text = Li.Text
                Okay = True
            End If
        Next
        If Not Okay Then
            T.Text = D.SelectedItem.Text
        End If
    End If
End Sub

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

by:emi_sastra
ID: 20416745
Hi Rob,

I don't set autopostback = "true", so it doesn't fired GridView1_RowUpdating event.
Actually I don't want to use autoposback.

I use txtQty_TextChanged(ByVal sender As Object, ByVal e As EventArgs), this event fired even there no autoposback='true" is set.

Thank you
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 20416785
Hi Grey,

First, your code has weakness, what would happened if the textbox value are all (some)  the same for all rows. In that case the code will get wrong value.

Second, there is easy way, just get the grid row, then get the dropdownlist selected value of the row.
The problem is I don't know how to get it in code.

Thank you.

0
 
LVL 16

Expert Comment

by:GreymanMSC
ID: 20417016
The value of the textbox is irrelevant to identifying the textbox.  The code always uses the textbox(s) that had triggered of the OnTextChange event.  It's value will be whatever it was to which was changed.  
 
The first two lines of the code identifies this sender and matches it to the drop down list that is in the same row.  What stuff you do with their content after that is up to you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 20417370
Sorry,

What is the usage of this code :

If Li.Text.Trim.Equals(T.Text.Trim) Then

Dim T As TextBox = CType(sender, TextBox)

Let say T="X"

What would be the Li.Text. ?
What would be the Li.Value?.

Why should be equal:  If Li.Text.Trim.Equals(T.Text.Trim) Then  to get the d.value?

Why set T.Text = Li.Text when Li.Text.Trim.Equals(T.Text.Trim) ?

I rather confuse here.

Thank you.
0
 
LVL 16

Expert Comment

by:GreymanMSC
ID: 20417486
It's just examples of how to access the information in the dropdownlist, since you didn't specify what you wanted to do with it after the textbox was updated.

So, what exactly do you want to do?
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 20417507
Hi Rob,

I set autoposback ="true", but Protected Sub grid_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles grid.RowUpdating

not fired.

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 20417645
Hi Greyman,

At last, I just add:

Dim strValue As String = D.SelectedValue

After below code

 Dim T As TextBox = CType(sender, TextBox)
    Dim D As DropDownList = T.Parent.FindControl("TheDropDownList")

And it solve my problem.

The complete code will be:

 Dim T As TextBox = CType(sender, TextBox)
    Dim D As DropDownList = T.Parent.FindControl("TheDropDownList")

Dim strValue As String = D.SelectedValue

The strValue then contain the value inside the TheDropDownList.

Sorry, I just confuse of the iteration, and still now.

Thank you very much anyway.





0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question