[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to get other control value at grid view?

Posted on 2007-12-05
10
Medium Priority
?
231 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

873 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