AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 Update Panel With Gridview Challenge -Calculate In Textbox
Posted by Teresa Cannon on  11/1/2007 1:07:18 PM Category: AJAX
Add to my favorites
Email to friend
ASP.NET 2.0, AJAX, VB.NET

I have two drop down lists and one text box in a gridview inside an AJAX update panel. The drop down lists values are 0-10.

I need the textbox to add the values of the textboxes without a page refresh, like an "onchange" event.

Here is code so far:

Calculate.aspx

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" ChildrenAsTriggers="true" runat="server">
<ContentTemplate>
<asp:GridView ID="gvMgrQuickScore" AlternatingRowStyle-BackColor="#EEEEEE" AutoGenerateColumns="false" EmptyDataText="You currently have no employees in your rollup" EmptyDataRowStyle-CssClass="validation1" runat="server">
<Columns>
<asp:TemplateField HeaderStyle-CssClass="datagridB" HeaderText="TE">
<ItemTemplate>

<asp:DropDownList ID="ddlQ1" runat="server" SelectedValue='<%#Eval("Q1")%>' Width="40" CssClass="datagrid" DataMember="Q1">
<asp:ListItem Value="0"></asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
<asp:ListItem Value="7">7</asp:ListItem>
<asp:ListItem Value="8">8</asp:ListItem>
<asp:ListItem Value="9">9</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderStyle-CssClass="datagridB" HeaderText="AN">
<ItemTemplate>
<asp:DropDownList ID="ddlQ2" runat="server" SelectedValue='<%#Eval("Q2")%>' Width="40" CssClass="datagrid" DataMember="Q2">
<asp:ListItem Value="0"></asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
<asp:ListItem Value="7">7</asp:ListItem>
<asp:ListItem Value="8">8</asp:ListItem>
<asp:ListItem Value="9">9</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:TextBox ID="txtTot" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>


CODE BEHIND - Calculate.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim uid As Integer = Session("UserID")
Dim OrgCode As String = Session("Org")

'For Testing Only
OrgCode = "1003540009"


If Not Page.IsPostBack Then
gvMgrQuickScore.DataSource = EAP_AssessmentDAC.EmpScoresByMgrGetAll(OrgCode, Left(OrgCode, 5))
gvMgrQuickScore.DataBind()
' BindData(OrgCode)
Page.DataBind()

End If

End Sub

Sub DoCalc()

For i As Integer = 0 To (gvMgrQuickScore.Rows.Count - 1)

Dim lEmoID As Label = gvMgrQuickScore.Rows(i).FindControl("lblEmpID")

For m As Integer = 1 To 9

Dim dQ1 As DropDownList = gvMgrQuickScore.Rows(i).FindControl("ddlQ1")
Dim dQ2 As DropDownList = gvMgrQuickScore.Rows(i).FindControl("ddlQ2")
Dim dQ3 As DropDownList = gvMgrQuickScore.Rows(i).FindControl("ddlQ3")
Dim tTot As TextBox = gvMgrQuickScore.Rows(i).FindControl("txtTot")

tTot.Text = CType(dQ1.SelectedValue, Integer) + CType(dQ2.SelectedValue, Integer) + CType(dQ3.SelectedValue, Integer)

Next

Next

End Sub



Protected Sub gvMgrQuickScore_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvMgrQuickScore.RowDataBound

DoCalc()

End Sub

How do I do this?

Post Your Reply Post New Question

 Viewer's Reply
Where is dQ3?
Answered By Moderator1 on 11/2/2007 9:36:32 AM
Hi,

First of all we cannot find dQ3 dropdown control.

Then we came to understand that you want to add both dropdown values and show the result in the Textbox. So you have to put the DoCalc() method on the SelectedIndexChange event of both the dropdowns. Make simple logic, sure you can work out the program.

Thanks.
 
I Took Out dQ3
Answered By Teresa Cannon on 11/2/2007 11:52:53 AM
I Took Out dQ3 and all references to it.

But how do I do a OnSelectedIndexChanged event on the drop down lists inside of the gridview?

When I add OnSelectedIndexChanged="DoCalc", I get this error:

Method 'Public Sub DoCalc()' does not have the same signature as delegate 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs)'.

Help!
 
Call DoCalc from ddlQ1 event.
Answered By Moderator1 on 11/3/2007 11:23:33 PM
you are assigning DoCalc method directly to OnSelectedIndexChanged. It requires all the parameters of selectedIndexChanged event. Use below is c# code and change it to your VB.NET.


protected void ddlQ1_SelectedIndexChanged(object sender, EventArgs e)
{
DoCalc();
}
 
How Do I Find OnSelectedIndexChanged?
Answered By Teresa Cannon on 11/5/2007 2:08:53 PM
How Do I Find OnSelectedIndexChanged for the ddlQ1?

I added this code and nothing is happening:

Protected Sub ddlQ1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
DoCalc()
End Sub

The ddlQ1 is inside of the gridview and I think I need to use the findcontrol method, but how do I do this to find the SelectedIndexChanged sub?
 
Just double click on Dropdown list
Answered By Moderator1 on 11/6/2007 9:37:35 PM
Hi,

just you double click on the dropdown box , it will create the event, the you can call the DoCalc() method.

protected void ddlQ1_SelectedIndexChanged(object sender, EventArgs e)
{
DoCalc();
}
 
SelectedIndexChanged on Gridview
Answered By Teresa Cannon on 11/7/2007 2:14:16 PM
When I did as you suggested, I get the SelectedIndexChanged for the gridview.

I tried this, but still not working.

Protected Sub gvMgrQuickScore_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvMgrQuickScore.SelectedIndexChanged

DoCalc()

End Sub

The code behind can't see the drop down lists without doing a findcontrol, I think.
 
Need Help as well!
Answered By kontr0l on 11/14/2007 2:32:11 PM
Same issue, can't figure it out yet....

How do you Handle the SelectedIndexChanged event for a dropdownlist inside of a gridview?

Did you find a solution?
 
FOUND IT!!
Answered By kontr0l on 11/14/2007 3:33:37 PM
Private dropdown As DropDownList

Protected Sub GridView1_Bound(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridView1.Load

dropdown = DirectCast(GridView1.HeaderRow.FindControl("dropdown"), DropDownList)

ddMonth.AutoPostBack = True

AddHandler ddMonth.SelectedIndexChanged, AddressOf ddMonth_SelectedIndexChanged

End Sub

then create a functino called ddMonth_SelectedIndexChanged like below:

Protected Sub ddMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Do what you want here..
End Sub
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
Introduction to Web Applications Development
Integrating Silverlight 4 with SharePoint 2010 - Free 42 Page Sample Chapter
Washington Technology