locked
multply 3 label and show in 4th label RRS feed

  • Question

  • User2033107836 posted

    Hello

    This is my code

    Label Showoutput = e.Item.FindControl("Labeloutput") as Label;
    
                
    Label v1 = ((Control)sender).Parent.FindControl("Value1") as Label;
    Label v2 = e.Item.FindControl("Value2") as Label;
    Label v3 = e.Item.FindControl("Value3") as Label;
    
    

    I want to multiply v1 * v2 / v3

    how to do so in simple format using asp.net 2.0 C#

    Wednesday, July 19, 2017 6:39 AM

Answers

  • User-1838255255 posted

    Hi asp.ambur,

    I checked your code writing is right, but I am not clear you how to write the front page code and which event you use. So I hope you could post your code for us

    to test. This is very important, also it could help you to solve your problem.

    Here are some tips for you to check you code:

    1. Check the ID if is right?

    2. Add breakpoint  to check if get the element(label)?

    I make a sample in repeater, please check:

    Sample Code:

    <asp:Repeater ID="Repeater1" runat="server"
                    OnItemDataBound="Repeater1_ItemDataBound">
                    <ItemTemplate>
                        <h1>Repeater 1</h1>
                        <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound">
                            <ItemTemplate>
                                <h1>Repeater 2</h1>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Value1") %>'></asp:Label>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Value2") %>'></asp:Label>
                                <asp:Label ID="Label3" runat="server" Text=""></asp:Label>
                            </ItemTemplate>
                        </asp:Repeater>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Value1") %>'></asp:Label>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Value2") %>'></asp:Label>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("Value3") %>'></asp:Label>
                    </ItemTemplate>
    </asp:Repeater>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                DataTable table = new DataTable();
                table.Columns.Add("Value1", typeof(string));
                table.Columns.Add("Value2", typeof(string));
                table.Columns.Add("Value3", typeof(string));
    
                // Here we add five DataRows.
                table.Rows.Add("25", "2", "5");
                table.Rows.Add("50", "4", "4");
                table.Rows.Add("10", "5", "3");
                table.Rows.Add("21", "5", "5");
                table.Rows.Add("100", "3", "6");
                Repeater1.DataSource = table;
                Repeater1.DataBind();
    
    
            }
            protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    Repeater Repeater2 = (Repeater)(e.Item.FindControl("Repeater2"));
    
                    DataTable dt2 = new DataTable();
                    dt2.Columns.Add("Value1", typeof(string));
                    dt2.Columns.Add("Value2", typeof(string));
    
                    // Here we add five DataRows.
                    dt2.Rows.Add("25", "2");
                    dt2.Rows.Add("50", "4");
                    dt2.Rows.Add("10", "5");
                    Repeater2.DataSource = dt2;
                    Repeater2.DataBind();
                }
            }
    
            protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    //Repeater Repeater2 = (Repeater)(e.Item.FindControl("Repeater2"));
                    Label label1 = e.Item.FindControl("Label1") as Label;
                    Label label2 = e.Item.FindControl("Label2") as Label;
                    Label label3 = e.Item.FindControl("Label3") as Label;
                    Label label4 = ((Control)sender).Parent.FindControl("Label2") as Label;
                    label3.Text = (Convert.ToDouble(label1.Text) * Convert.ToDouble(label2.Text) / Convert.ToDouble(label4.Text)).ToString();
                }
            }

    Best Regards,

    Eric Du


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 19, 2017 9:32 AM

All replies

  • User-1716253493 posted

    Something like this

    showoutput.Text = (Convert.ToDouble(v1.Text) * Convert.ToDouble(v2.Text) / Convert.ToDouble(v3.Text)).ToString();

    Wednesday, July 19, 2017 7:03 AM
  • User2033107836 posted

    getting error

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 85:             Label lq = e.Item.FindControl("Qty") as Label;
    Line 86: 
    Line 87:             labeloutput.Text = (Convert.ToDouble(ls.Text) * Convert.ToDouble(lr.Text) / Convert.ToDouble(lq.Text)).ToString();
    Line 88: 
    Line 89:         }
    Wednesday, July 19, 2017 7:26 AM
  • User-1716253493 posted

    the error indicate that your are not getting the controls

    Wednesday, July 19, 2017 7:33 AM
  • User-1838255255 posted

    Hi asp.ambur,

    According to your code, I make a sample for your reference, here is the sample code, please check:

    Sample Code:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Value1") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Value2") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Value3") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Label ID="Label4" runat="server" Text=""></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
    </asp:GridView>
    
    
    protected void Page_Load(object sender, EventArgs e)
            {
                DataTable table = new DataTable();
                table.Columns.Add("Value1", typeof(string));
                table.Columns.Add("Value2", typeof(string));
                table.Columns.Add("Value3", typeof(string));
    
                // Here we add five DataRows.
                table.Rows.Add("25", "2", "5");
                table.Rows.Add("50", "4", "4");
                table.Rows.Add("10", "5", "3");
                table.Rows.Add("21", "5", "5");
                table.Rows.Add("100", "3", "6");
                GridView1.DataSource = table;
                GridView1.DataBind();
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Label a = e.Row.FindControl("Label1") as Label;
                    Label b = e.Row.FindControl("Label2") as Label;
                    Label c = e.Row.FindControl("Label3") as Label;
                    Label d = e.Row.FindControl("Label4") as Label;
                    d.Text = (Convert.ToInt32(a.Text) * Convert.ToInt32(b.Text) / Convert.ToInt32(c.Text)).ToString();
                }
            }

    About your new error, I think it ,may be caused by not get the label. I hope you could post the complete code(Control code and event code) for us to test.

    Best Regards,

    Eric Du

    Wednesday, July 19, 2017 7:45 AM
  • User2033107836 posted

    hello Eric

    Im using repater control and partent and child repater will this 

    Label Showoutput = e.Item.FindControl("Labeloutput") as Label;
    
                
    Label v1 = ((Control)sender).Parent.FindControl("Value1") as Label;
    Label v2 = e.Item.FindControl("Value2") as Label;
    Label v3 = e.Item.FindControl("Value3") as Label;
    Wednesday, July 19, 2017 8:28 AM
  • User2033107836 posted

    Something like this

    showoutput.Text = (Convert.ToDouble(v1.Text) * Convert.ToDouble(v2.Text) / Convert.ToDouble(v3.Text)).ToString();

    due to my parent repater is like this 

    Label v1 = ((Control)sender).Parent.FindControl("Value1") as Label;

    how to access that repater in v3.text

    Wednesday, July 19, 2017 9:24 AM
  • User-1838255255 posted

    Hi asp.ambur,

    I checked your code writing is right, but I am not clear you how to write the front page code and which event you use. So I hope you could post your code for us

    to test. This is very important, also it could help you to solve your problem.

    Here are some tips for you to check you code:

    1. Check the ID if is right?

    2. Add breakpoint  to check if get the element(label)?

    I make a sample in repeater, please check:

    Sample Code:

    <asp:Repeater ID="Repeater1" runat="server"
                    OnItemDataBound="Repeater1_ItemDataBound">
                    <ItemTemplate>
                        <h1>Repeater 1</h1>
                        <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound">
                            <ItemTemplate>
                                <h1>Repeater 2</h1>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Value1") %>'></asp:Label>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Value2") %>'></asp:Label>
                                <asp:Label ID="Label3" runat="server" Text=""></asp:Label>
                            </ItemTemplate>
                        </asp:Repeater>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Value1") %>'></asp:Label>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Value2") %>'></asp:Label>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("Value3") %>'></asp:Label>
                    </ItemTemplate>
    </asp:Repeater>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                DataTable table = new DataTable();
                table.Columns.Add("Value1", typeof(string));
                table.Columns.Add("Value2", typeof(string));
                table.Columns.Add("Value3", typeof(string));
    
                // Here we add five DataRows.
                table.Rows.Add("25", "2", "5");
                table.Rows.Add("50", "4", "4");
                table.Rows.Add("10", "5", "3");
                table.Rows.Add("21", "5", "5");
                table.Rows.Add("100", "3", "6");
                Repeater1.DataSource = table;
                Repeater1.DataBind();
    
    
            }
            protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    Repeater Repeater2 = (Repeater)(e.Item.FindControl("Repeater2"));
    
                    DataTable dt2 = new DataTable();
                    dt2.Columns.Add("Value1", typeof(string));
                    dt2.Columns.Add("Value2", typeof(string));
    
                    // Here we add five DataRows.
                    dt2.Rows.Add("25", "2");
                    dt2.Rows.Add("50", "4");
                    dt2.Rows.Add("10", "5");
                    Repeater2.DataSource = dt2;
                    Repeater2.DataBind();
                }
            }
    
            protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    //Repeater Repeater2 = (Repeater)(e.Item.FindControl("Repeater2"));
                    Label label1 = e.Item.FindControl("Label1") as Label;
                    Label label2 = e.Item.FindControl("Label2") as Label;
                    Label label3 = e.Item.FindControl("Label3") as Label;
                    Label label4 = ((Control)sender).Parent.FindControl("Label2") as Label;
                    label3.Text = (Convert.ToDouble(label1.Text) * Convert.ToDouble(label2.Text) / Convert.ToDouble(label4.Text)).ToString();
                }
            }

    Best Regards,

    Eric Du


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 19, 2017 9:32 AM