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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 974
  • Last Modified:

OnTextChanged event

Hi everyone,

I need to figure out a way to pull a value from a textbox from a OnTextCharge event.

Here is a copy of my Code behind:

Public Sub DetermindNewGM(ByVal Source As Object, ByVal E As EventArgs)

        Try

            Dim dgi As DataGridItem

            Dim test = Convert.ToString(CInt(2 + 2))
            Response.Write("<script> alert('" + test + "') </script>")

            Dim Textbox1 As String = CType(dgi.FindControl(Textbox1), TextBox).Text


            myLabel.Text = CType(dgi.FindControl("NewPriceLevel"), TextBox).Text


        Catch
            Response.Write("<script> alert('There was an error!') </script>")
        End Try

    End Sub

When ran, after I enter a value into the Textbox1, I get a alert of "4" then "There was an error".  Otherwise I don't see the value I put into Textbox1.

Thx,

M
0
muligan
Asked:
muligan
  • 26
  • 14
  • 2
1 Solution
 
stengeljCommented:
This might give you more descriptive information about your error.
...
Catch ex As Exception
     Response.Write("<script> alert('Error: " + ex + "') </script>")
...
0
 
muliganAuthor Commented:
no help...thx thou
0
 
stengeljCommented:
Try changing these two things:

This: Response.Write("<script> alert('" + test + "') </script>")
To this: Response.Write("<script" + "> alert('" + test + "') <" + "/script>")

And

This: Response.Write("<script> alert('There was an error!') </script>")
To This: Response.Write("<script" + "> alert('Error: " + ex.ToString + "') <" + "/script>")

I think you need to split up those <script> tags so they don't get read as real <script> tags in your code.  You might be better off using labels for your debugs--just a thought.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
muliganAuthor Commented:
I have this sort of logic working outside of a datagrid:

Sub change(ByVal sender As Object, ByVal e As EventArgs)

        If txt1.Text <> "" AndAlso txt2.Text <> "" Then
            lbl2.Text = CInt(CInt(txt1.Text) + CInt(txt2.Text))
        End If
End Sub

But this logic will not work inside of a datagrid.

Thoughts?

M
0
 
muliganAuthor Commented:
Increased points to 500 points.
0
 
muliganAuthor Commented:
I'm getting the old error:

Object reference not set to an instance of an object

I would assume that my Findcontrol is not being found or return nothing?

Here is my current code:

Public Sub NewGM_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewMultiplier.TextChanged
        Try

            Dim dgi As DataGridItem

            Dim textBox1 As TextBox = CType(dgi.FindControl("NewPriceLevel"), TextBox)

            If textBox1 Is Nothing Then
                lbl2.Text = "Unexpected condition"
            Else
                lbl2.Text = textBox1.Text
            End If

        Catch ex As Exception
            lbl2.Text = "Error: " & ex.ToString
        End Try


    End Sub
0
 
b1xml2Commented:
Public Sub TextBox_Changed(ByVal sender As Object, ByVal e As EventArgs)
        Dim gridItem As DataGridItem = DirectCast(DirectCast(sender, Control).BindingContainer, DataGridItem)
        Dim txtColumnA As TextBox = DirectCast(gridItem.FindControl("txtColumnA"), TextBox)
        Dim txtColumnB As TextBox = DirectCast(gridItem.FindControl("txtColumnB"), TextBox)


End Sub

on the assumption that the TextBox looks like this in the DataGrid

    <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" ShowFooter="True">
    <Columns>
    <asp:TemplateColumn>
    <ItemStyle HorizontalAlign="Right"></ItemStyle>
    <FooterStyle HorizontalAlign="Right"></FooterStyle>
    <HeaderStyle HorizontalAlign="Right"></HeaderStyle>
    <HeaderTemplate>A</HeaderTemplate>
    <ItemTemplate><%# DataBinder.Eval(Container.DataItem,"ColumnA","{0:N0}" )%></ItemTemplate>
    <EditItemTemplate>
            <asp:RequiredFieldValidator Id="rfvColumnA" Runat="server" Display="Dynamic" ControlToValidate="txtColumnA"><b style="cursor:hand;color:red;" title="Required">**</b></asp:RequiredFieldValidator>
          <asp:RangeValidator Id="rvColumnA" runat="server" Display="Dynamic" ControlToValidate="txtColumnA" Type="Integer" MinimumValue="0" MaximumValue="100000"><b style="cursor:hand;color:red;" title="Between 1 and 100,000">**</b></asp:RangeValidator>
          <asp:TextBox Id="txtColumnA" runat="server" style="text-align:right" OnTextChanged="TextBox_Changed" AutoPostBack="True" Text='<%# DataBinder.Eval(Container.DataItem,"ColumnA","{0:N0}" )%>' />
    </EditItemTemplate>
    <FooterTemplate>
            <asp:RequiredFieldValidator Id="rfvColumnA" Runat="server" Display="Dynamic" ControlToValidate="txtColumnA"><b style="cursor:hand;color:red;" title="Required">**</b></asp:RequiredFieldValidator>
          <asp:RangeValidator Id="rvColumnA" runat="server" Display="Dynamic" ControlToValidate="txtColumnA" Type="Integer" MinimumValue="0" MaximumValue="100000"><b style="cursor:hand;color:red;" title="Between 1 and 100,000">**</b></asp:RangeValidator>
            <asp:TextBox Id="txtColumnA" style="text-align:right" runat="Server" OnTextChanged="TextBox_Changed" AutoPostBack="True"/></FooterTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <ItemStyle HorizontalAlign="Right"></ItemStyle>
    <FooterStyle HorizontalAlign="Right"></FooterStyle>
    <HeaderStyle HorizontalAlign="Right"></HeaderStyle>
    <HeaderTemplate>B</HeaderTemplate>
    <ItemTemplate><%# DataBinder.Eval(Container.DataItem,"ColumnB","{0:N0}") %></ItemTemplate>
    <EditItemTemplate>
            <asp:RequiredFieldValidator Id="rfvColumnB" Runat="server" Display="Dynamic" ControlToValidate="txtColumnB"><b style="cursor:hand;color:red;" title="Required">**</b></asp:RequiredFieldValidator>
          <asp:RangeValidator Id="rvColumnB" runat="server" Display="Dynamic" ControlToValidate="txtColumnB" Type="Integer" MinimumValue="0" MaximumValue="100000"><b style="cursor:hand;color:red;" title="Between 1 and 100,000">**</b></asp:RangeValidator>
            <asp:TextBox Id="txtColumnB" style="text-align:right" runat="server" OnTextChanged="TextBox_Changed" AutoPostBack="True" Text='<%# DataBinder.Eval(Container.DataItem,"ColumnB","{0:N0}" )%>' />
      </EditItemTemplate>
    <FooterTemplate>
          <asp:RequiredFieldValidator Id="rfvColumnB" Runat="server" Display="Dynamic" ControlToValidate="txtColumnB"><b style="cursor:hand;color:red;" title="Required">**</b></asp:RequiredFieldValidator>
          <asp:RangeValidator Id="rvColumnB" runat="server" Display="Dynamic" ControlToValidate="txtColumnB" Type="Integer" MinimumValue="0" MaximumValue="100000"><b style="cursor:hand;color:red;" title="Between 1 and 100,000">**</b></asp:RangeValidator>
            <asp:TextBox Id="txtColumnB" style="text-align:right" runat="Server" OnTextChanged="TextBox_Changed" AutoPostBack="True"/>
      </FooterTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="ColumnC" DataFormatString="{0:N0}" ReadOnly="True" HeaderText="C">
    <ItemStyle HorizontalAlign="Right" Width="100"></ItemStyle>
    <HeaderStyle HorizontalAlign="Right" Width="100"></HeaderStyle>
    </asp:BoundColumn>  
    <asp:TemplateColumn>
    <ItemStyle HorizontalAlign="Right"></ItemStyle>
    <FooterStyle HorizontalAlign="Right"></FooterStyle>
    <ItemTemplate>
            <asp:LinkButton Id="DeleteButton" runat="Server" CommandName="remove" CausesValidation="False">[remove]</asp:LinkButton>&nbsp;&nbsp;
            <asp:LinkButton Id="EditButton" runat="Server" CommandName="edit" CausesValidation="False">[edit]</asp:LinkButton>&nbsp;&nbsp;
      </ItemTemplate>
      <EditItemTemplate>
            <asp:LinkButton Id="BackButton" runat="Server" CommandName="back" CausesValidation="False">[back]</asp:LinkButton>&nbsp;&nbsp;
            <asp:LinkButton Id="UpdateButton" runat="Server" CommandName="update" CausesValidation="False">[update]</asp:LinkButton>&nbsp;&nbsp;
      </EditItemTemplate>
      <FooterTemplate>
            <asp:LinkButton Id="AddButton" runat="Server" CommandName="add" CausesValidation="False">[add]</asp:LinkButton>&nbsp;&nbsp;
      </FooterTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
0
 
muliganAuthor Commented:
This seems to at least take care of the errors:

Dim gridItem As DataGridItem = DirectCast(DirectCast(sender, Control).BindingContainer, DataGridItem)

Can you explain what is going on in that line of code?
0
 
muliganAuthor Commented:
I had:

Dim gridItem as DataGridItem

and that was causing the errors.
0
 
muliganAuthor Commented:
ok... the label works and displays what was in the textbox, but when I move that label inside of a datagrid, I get the error:

Object reference not set to an instance of an object....again

Thoughts?
0
 
muliganAuthor Commented:
Do I have to rebind the data with the new label?
0
 
b1xml2Commented:
every control has a BindingContainer. The sender which is the TextBox has as its BindingContainer the DataGridItem.

From getting the reference of the DataGridItem, we then get the reference for the other Controls...

When you move the label inside the DataGrid, it can only be referenced via the DataGridItem...
as in
Dim lbl As Label = DirectCast(gridItem.FindControl("<name_of_label>"),Label)
...
0
 
muliganAuthor Commented:
Sorry...I don't think we are talking about the same thing.

As far as putting (not reading) the label value back into the datagrid, as in your example from yesterday (ColumnC).  To be honest, that whole datagrid example you made up is a bit confusing.
0
 
muliganAuthor Commented:
Another different between your finished example and my project, is that I already have a populated datagrid.  It is a fully editable datagrid, were your example just adds on on at a time.  Is there an email address I can send a screen shot of my datagrid to you?
0
 
b1xml2Commented:
mulligan, keep that example and study it. It will not be understood overnight. But it has advanced elements of coding and programming. Then continue with your datagrid efforts. You will not find that example of mine on any of the so-called .NET websites as this code reflects real-life considerations and not sample code.

 I understand and accept that you want a watered down example, but sometimes it is good to go over the bar and then scale back as we are apparently doing so now.

What then is your current DataGrid HTML and your code behind?!
0
 
b1xml2Commented:
it removes and edits as well.... not just add. And if there is existing data, it amends the DataRowState accordingly.
0
 
muliganAuthor Commented:
They are pretty long, is it possible to email you?  And then post the final solution on here later?
0
 
b1xml2Commented:
it is against the rules to do that. Best just post the code here or put it up in a public place and post the url to it. After all there are other experts here, and they should also be given a fair chance to help you out.
0
 
muliganAuthor Commented:
Just as an FYI... in your example that you provided.  Sorry...I meant to say, that there is Add, Edit, Delete, but when you go to edit something the ItemCreated event doesn't work, you have to click the update link.  So, it wasn't really useful to migrate to my project.
0
 
muliganAuthor Commented:
I guess, but you already earned the points...;-)
0
 
muliganAuthor Commented:
HTML:
______________________

<form name="form1" runat="server" ID="Form1" enctype="multipart/form-data">

                  

                  <h2><asp:Label ID="lblTitle" runat="server" /></h2>
                  <asp:Label id="lblAccountManager" Visible="false" runat="server" />
                  <table cellpadding="4" border="0">
                        <tr>
                              <td>
                                    <asp:Button ID="btnDone" Text="Done" OnClick="Done" runat="server" CausesValidation="false"></asp:Button>
                              </td>
                        </tr>
                  </table>
                  <p>
                        <asp:DataGrid ID="dgContracts" AllowPaging="false" AllowSorting="true" AutoGenerateColumns="false"
                              CellPadding="6" CellSpacing="0" runat="server" ShowHeader="true" showfooter="true">
                              <HeaderStyle CssClass="TableTitle" />
                              <ItemStyle BackColor="#F2F2F2" Font-Name="Verdana, Arial, Helvetica, sans-serif" />
                              <AlternatingItemStyle BackColor="#E5E5E5" Font-Name="Verdana, Arial, Helvetica, sans-serif" />
                              <FooterStyle HorizontalAlign="center" BackColor="#E8EBFD" ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif"
                                    Font-Bold="true" />
                              <PagerStyle BackColor="white" Font-Name="Verdana, Arial, Helvetica, sans-serif" />
                              <Columns>
                                    <asp:TemplateColumn HeaderText="Delete" HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center"
                                          ItemStyle-HorizontalAlign="center" Visible="true">
                                          <FooterTemplate>
                                                <asp:Button id="btnAddRow" runat="server" Text="Add" CommandName="AddANewRow"></asp:Button>
                                          </FooterTemplate>
                                          <ItemTemplate>
                                                <asp:Button CommandName="Delete" Text="Delete" ID="btnDel" Runat="server" CausesValidation="false" />
                                          </ItemTemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" Visible="False"
                                          HeaderText="Customer" ItemStyle-HorizontalAlign="center">
                                          <itemtemplate>
                                                <asp:TextBox Visible=False Text='<%# Container.DataItem("Customer") %>' runat="server" ID="Customer" />
                                          </itemtemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" HeaderText="Supplier Code"
                                          ItemStyle-HorizontalAlign="center" Visible="true">
                                          <itemtemplate>
                                                <asp:Label Text='<%# Container.DataItem("Supplier") %>' runat="server" ID="lblSupplier" />
                                          </itemtemplate>
                                          <FooterTemplate>
                                                <asp:TextBox ID="add_Supplier" Columns="10" Runat="Server" />
                                          </FooterTemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" Visible="False"
                                          HeaderText="Supplier Code" ItemStyle-HorizontalAlign="center">
                                          <itemtemplate>
                                                <asp:TextBox Visible=False Text='<%# Container.DataItem("Supplier") %>' runat="server" ID="Supplier" />
                                          </itemtemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" HeaderText="Item #"
                                          ItemStyle-HorizontalAlign="center" Visible="true">
                                          <itemtemplate>
                                                <asp:Label Text='<%# Container.DataItem("Item") %>' runat="server" ID="lblItem" />
                                          </itemtemplate>
                                          <FooterTemplate>
                                                <asp:TextBox ID="add_Item" Columns="10" Runat="Server" />
                                          </FooterTemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" Visible="False"
                                          HeaderText="Supplier Code" ItemStyle-HorizontalAlign="center">
                                          <itemtemplate>
                                                <asp:TextBox Visible=False Text='<%# Container.DataItem("Item") %>' runat="server" ID="Item" />
                                          </itemtemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" HeaderText="Price Level"
                                          ItemStyle-HorizontalAlign="center" Visible="true">
                                          <itemtemplate>
                                                <asp:Label Text='<%# Container.DataItem("PriceLevel") %>' runat="server" ID="lblPriceLevel" />
                                          </itemtemplate>
                                          <FooterTemplate>
                                                <asp:TextBox ID="add_PriceLevel" Columns="10" Runat="Server" />
                                          </FooterTemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" Visible="False"
                                          HeaderText="Supplier Code" ItemStyle-HorizontalAlign="center">
                                          <itemtemplate>
                                                <asp:TextBox Visible=False Text='<%# Container.DataItem("PriceLevel") %>' runat="server" ID="PriceLevel" />
                                          </itemtemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" HeaderText="Multiplier"
                                          ItemStyle-HorizontalAlign="center" Visible="true">
                                          <itemtemplate>
                                                <asp:Label Text='<%# Container.DataItem("Multiplier") %>' runat="server" ID="lblMultiplier" />
                                          </itemtemplate>
                                          <FooterTemplate>
                                                <asp:TextBox ID="add_Multiplier" Columns="10" Runat="Server" />
                                          </FooterTemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" Visible="False"
                                          HeaderText="Supplier Code" ItemStyle-HorizontalAlign="center">
                                          <itemtemplate>
                                                <asp:TextBox Visible=False Text='<%# Container.DataItem("Multiplier") %>' runat="server" ID="Multiplier" />
                                          </itemtemplate>
                                    </asp:TemplateColumn>
                                    <asp:BoundColumn HeaderText="OR" HeaderStyle-HorizontalAlign="center" HeaderStyle-BackColor="#FFFFCC"
                                          ItemStyle-BackColor="#FFFFCC" ItemStyle-HorizontalAlign="center" ReadOnly="true" Visible="true" />
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" HeaderText="Fixed Price"
                                          ItemStyle-HorizontalAlign="center" Visible="true">
                                          <itemtemplate>
                                                <asp:Label Text='<%# FormatCurrency(Container.DataItem("Fixed Price")) %>' runat="server" ID="lblFixedPrice" />
                                          </itemtemplate>
                                          <FooterTemplate>
                                                <asp:TextBox ID="add_FixedPrice" Columns="10" Runat="Server" />
                                          </FooterTemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" Visible="False"
                                          HeaderText="FixedPrice" ItemStyle-HorizontalAlign="center">
                                          <itemtemplate>
                                                <asp:TextBox Visible=False Text='<%# Container.DataItem("Fixed Price") %>' runat="server" ID="FixedPrice"/>
                                          </itemtemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" HeaderText="Cost"
                                          ItemStyle-HorizontalAlign="center" Visible="true">
                                          <itemtemplate>
                                                <asp:Label Text='<%#  DataBinder.Eval(Container, "DataItem.Cost", "{0:f4}") %>' runat="server" ID="lblCost" />
                                          </itemtemplate>
                                          <FooterTemplate>
                                                <asp:TextBox ID="add_Cost" Columns="10" Runat="Server" />
                                          </FooterTemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" Visible="False"
                                          HeaderText="Cost" ItemStyle-HorizontalAlign="center">
                                          <itemtemplate>
                                                <asp:TextBox Visible=False Text='<%#  DataBinder.Eval(Container, "DataItem.Cost", "{0:f4}") %>' runat="server" ID="Cost"/>
                                          </itemtemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" HeaderText="Current<br><b>GM</b>"
                                          ItemStyle-HorizontalAlign="center" Visible="true">
                                          <itemtemplate>
                                                <asp:Label Text='<%# DataBinder.Eval(Container, "DataItem.CurrentGM", "{0:p1}") %>' runat="server" ID="lblCurrentGM"/>
                                          </itemtemplate>
                                          <FooterTemplate>
                                          </FooterTemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" Visible="False"
                                          HeaderText="Current<br><b>GM</b>" ItemStyle-HorizontalAlign="center">
                                          <itemtemplate>
                                                <asp:TextBox Visible=False Text='<%# Container.DataItem("CurrentGM") %>' runat="server" ID="CurrentGM"/>
                                          </itemtemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" Visible="False"
                                          HeaderText="EffectivePrice" ItemStyle-HorizontalAlign="center">
                                          <itemtemplate>
                                                <asp:TextBox Visible=False Text='<%# Container.DataItem("EffectivePrice") %>' runat="server" ID="EffectivePrice"/>
                                          </itemtemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderStyle-BackColor="#6699CC" HeaderStyle-HorizontalAlign="center" Visible="False"
                                          HeaderText="EffectiveCost" ItemStyle-HorizontalAlign="center">
                                          <itemtemplate>
                                                <asp:TextBox Visible=False Text='<%# Container.DataItem("EffectiveCost") %>' runat="server" ID="EffectiveCost"/>
                                          </itemtemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderText="New<br>Price Level" HeaderStyle-BackColor="#FFCC66" HeaderStyle-HorizontalAlign="center"
                                          ItemStyle-HorizontalAlign="center">
                                          <itemtemplate>
                                                <asp:TextBox ID="NewPriceLevel" size="10" runat="server" OnTextChanged="NewGM_TextChanged"  />
                                          </itemtemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderText="New<br>Multiplier" HeaderStyle-BackColor="#FFCC66" HeaderStyle-HorizontalAlign="center"
                                          ItemStyle-HorizontalAlign="center">
                                          <itemtemplate>
                                                <asp:TextBox ID="NewMultiplier" size="10" runat="server" AutoPostBack="True" OnTextChanged="NewGM_TextChanged" />
                                          </itemtemplate>
                                    </asp:TemplateColumn>
                                    <asp:BoundColumn HeaderText="OR" HeaderStyle-HorizontalAlign="center" HeaderStyle-BackColor="#FFFFCC"
                                          ItemStyle-BackColor="#FFFFCC" ItemStyle-HorizontalAlign="center" ReadOnly="true" Visible="true" />
                                    <asp:TemplateColumn HeaderText="New<br>Fixed Price" HeaderStyle-BackColor="#FFCC66" HeaderStyle-HorizontalAlign="center"
                                          ItemStyle-HorizontalAlign="center">
                                          <itemtemplate>
                                                <asp:TextBox ID="NewFixedPrice" size="10" Text='' runat="server" />
                                          </itemtemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderText="New<br>Cost" HeaderStyle-BackColor="#FFCC66" HeaderStyle-HorizontalAlign="center"
                                          ItemStyle-HorizontalAlign="center">
                                          <itemtemplate>
                                                <asp:TextBox ID="NewCost" size="10" Text='' runat="server" />
                                          </itemtemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderText="New<br><b>GM</b>" HeaderStyle-BackColor="#FFCC66" HeaderStyle-HorizontalAlign="center"
                                          ItemStyle-HorizontalAlign="center">
                                          <itemtemplate>
                                                <p><asp:Label id="lbl2" runat="server" /></p>
                                          </itemtemplate>
                                    </asp:TemplateColumn>
                              </Columns>
                        </asp:DataGrid>
                  </p>
                  <P>
                        <asp:Button id="btnUpdateAll" runat="server" CssClass="hb9" Text="Submit"
                              CausesValidation="false"></asp:Button>
                  </P>
                  <P>
                        <asp:label Font-Bold="true" ForeColor="#FF0000" ID="lblSubmitted" runat="server"></asp:label></P>
            </form>
0
 
b1xml2Commented:
the DataGrid example I provided uses the lazy update model in that it stores all changes until the user is ready. I did not provide the Save button as that would involved ADO.NET and I did not want to make things more complicated

Most of the examples on the NET uses the instant update/insert/delete model. Which while it is fine for small projects really is not employed in the bigger ones.
0
 
muliganAuthor Commented:
Code Behind:

Public Sub NewGM_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
        Try

            Dim gridItem As DataGridItem = DirectCast(DirectCast(sender, Control).BindingContainer, DataGridItem)

            Dim textBox1 As TextBox = DirectCast(gridItem.FindControl("NewPriceLevel"), TextBox)
            Dim textBox2 As TextBox = DirectCast(gridItem.FindControl("NewMultiplier"), TextBox)

            If Not textBox1.Text Is Nothing AndAlso Not textBox2.Text Is Nothing Then
                lbl2.Text = CInt(CInt(textBox1.Text) + CInt(textBox2.Text))
            Else
                lbl2.Text = "Error"
            End If


        Catch ex As Exception
            lbl2.Text = "Error: "
        End Try

    End Sub
0
 
b1xml2Commented:
 If Not textBox1.Text Is Nothing AndAlso Not textBox2.Text Is Nothing Then
                lbl2.Text = (CInt(textBox1.Text) + CInt(textBox2.Text)).ToString
            Else
                lbl2.Text = "Error"
            End If
0
 
muliganAuthor Commented:
As you can see from my datagrid,  I have a datagrid being returned, but I am adding four additional columns on the end of the datagrid to allow the user to input New Values.  Well, I have to take those new values and do some calculation on them.

So, I would like to pull the value from the four textboxes and do my caculations and put that calculation into the label at the very end of the datagrid.
0
 
muliganAuthor Commented:
When I add .ToString,

I get the error: Object reference not set to an instance of an object.
0
 
muliganAuthor Commented:
As soon as I simply move the Label: <p><asp:Label id="lbl2" runat="server" /></p>

And throw it at the top of the page, it works fine.
0
 
b1xml2Commented:
Public Sub NewGM_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
      Try
      
      Dim gridItem As DataGridItem = DirectCast(DirectCast(sender, Control).BindingContainer, DataGridItem)
      Dim textBox1 As TextBox = DirectCast(gridItem.FindControl("NewPriceLevel"), TextBox)
      Dim textBox2 As TextBox = DirectCast(gridItem.FindControl("NewMultiplier"), TextBox)
      Dim lbl2 As Label = DirectCast(gridItem.FindControl("lbl2"),Label)
      
      If Not textBox1 Is Nothing AndAlso Not textBox2 Is Nothing AndAlso Not lbl2 Is Nothing Then
            Dim value1 As Double = 0
            Dim value2 As Double = 0
            If Double.TryParse(textBox1.Text, Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture, value1) AndAlso _
                  Double.TryParse(textBox2.Text, Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture, value2) Then
                  Dim total As Integer = CInt( (value1 + value2))
                  lbl2.Text = total.ToString
            Else
                  lbl2.Text = "Error"
            End If
      End If      
      
      Catch ex As Exception
      
      End Try

End Sub
   
0
 
muliganAuthor Commented:
Yahoo!!!!!!!!!!!!

It worked,  can you explain what/why you did what you did?  I've been banging my head against the wall for two days on this.

Thanks a ton!

Jeff
0
 
b1xml2Commented:
lol!

okay here's the thing, if your label is inside the DataGrid, you must access the GridItem explicitly before you can even hope of getting the reference to the label.
Use the Double.TryParse method() it doesnt raise an error is the text is not numeric. It returns a True (parsed) or False (cannot parse). It also passes ByReference the parsed value to the variable. We need though to pass information about the current culture (locale which affects date and numeric styling). If both functions return true then both text boxes have numeric values and we just add them

You can use the IsNumeric but that will not work well with say $3,000.00

0
 
muliganAuthor Commented:
I don't have to use currency at the moment, but I can see a use for it in the future?  What would I use in that case?

In this case, I have to do calculation to find a percentage, do you see any problems?

Thx,
0
 
b1xml2Commented:
you can do this:

Dim value As Decimal = 34.5
txtBox.Text = value.ToString("C")
0
 
muliganAuthor Commented:
Quick Question for ya:

If I need to make the new calculation a percentage, how would I do that?

lbl2.Text = total.ToString{0:p1}
0
 
b1xml2Commented:
yep

{0:P0} mean percentage with 0 decimal places

{0:P<decimal_places>}

like 10.02% would be {0:P2}

the % symbol would be outputted.
0
 
muliganAuthor Commented:
Sorry...I meant..

ToString{0:p1} doesn't work.  I assume the formatting is wrong.
0
 
b1xml2Commented:
ahhh
read too fast

ToString("P0") would output 10% if the value is 0.1

ToString("P2") would output 10.34 if the value is 01034

0
 
b1xml2Commented:
The {0:P2} would apply to DataBinder.Eval as well as String.Format, StringBuilder.AppendFormat
0
 
muliganAuthor Commented:
I must be missing something?  Here is my code:

lbl2.Text = total.ToString("P2")

And...I only get "0" in my datagrid.  Shouldn't I see "0%"?

Thx!

M
0
 
b1xml2Commented:
you need the total to be of type decimal or double
and the value should be less than 1 like 0.34
0
 
muliganAuthor Commented:
Hello,

I'm back again with a new problem for the same solution.  I am thinking about creating a new question about doing what I need to above, but doing it with javascript on the client side.  Are you familiar with javascript and if it is possible?

Thanks,

M
0
 
muliganAuthor Commented:
fyi...may be easy points for ya?  I like your solutions and explainations.

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21428760.html

0
 
muliganAuthor Commented:
Hi b1xml2...

Here is another question I have going if your interested?

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21434940.html

0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 26
  • 14
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now