Noonit
Noonit

Reputation: 11

How to change/get the text inside a textbox that is in a datalist

I need to get

1. the text from one textbox 
2. according to its value - change the text in another textbox

 all of them INSIDE a datalist.

(more specific: one textbox is getting the amount of items, and the second one needs to present the price. If one unit costs 20$ then when the user types in "2" in the amount textbox it'll present 40$ in the price textbox).

Problem is - i dont how to receive the amount from one textbox, neither changing the value of the second (price) textbox. Can you please help?

ty so muce in advanced ! :-)

I've stried with the ItemCommand in the datalist - problem is that I don't need a button in there so I can't call that commnad.

I've also tried to add a function 'on text changed' to the first textbox, still hadn't know how to proceed further after that.

    <asp:DataList ID="dlOrders" runat="server" RepeatColumns="2" 
                  oncancelcommand="dlOrders_CancelCommand" 
                  oneditcommand="dlOrders_EditCommand" 
                  onselectedindexchanged="dlOrders_SelectedIndexChanged" 
                  onupdatecommand="dlOrders_UpdateCommand"  >
           <ItemTemplate>
           <table>
    <tr>
        <td rowspan="4" style="background-color:black">
            <asp:Image ID="flightImg" runat="server" Height="80" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"Pic","Pics/{0}") %>' />
        </td>
        <td>
            <asp:Label ID="txt_id" runat="server" Text="Flight ID" Font-Bold="true" Font-Underline="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label1" runat="server" Text=":" Font-Bold="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="lbl_id" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"FightID") %>'></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="txt_date" runat="server" Text="Flight Date" Font-Bold="true" Font-Underline="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label2" runat="server" Text=":" Font-Bold="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="lbl_date" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Date") %>'></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="txt_dest" runat="server" Text="Destination" Font-Bold="true" Font-Underline="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label3" runat="server" Text=":" Font-Bold="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="lbl_dest" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Des") %>'></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="txt_out" runat="server" Text="Outlet" Font-Bold="true" Font-Underline="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label4" runat="server" Text=":" Font-Bold="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="lbl_out" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Outlet") %>'></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="btn_Order" runat="server" Text="Order" CommandName="Edit" />
        </td>
        <td>
            <asp:Label ID="txt_dur" runat="server" Text="Duration" Font-Bold="true" Font-Underline="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label5" runat="server" Text=":" Font-Bold="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="lbl_dur" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Duration") %>'></asp:Label>
        </td>
    </tr>
    <tr>
        <td></td>
        <td>
            <asp:Label ID="txt_kind" runat="server" Text="Kind" Font-Bold="true" Font-Underline="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label6" runat="server" Text=":" Font-Bold="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="lbl_kind" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Kind") %>'></asp:Label>
        </td>
    </tr>
    <tr>
        <td></td>
        <td>
            <asp:Label ID="txt_price" runat="server" Text="Price" Font-Bold="true" Font-Underline="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label8" runat="server" Text=":" Font-Bold="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="lbl_price" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Price") %>'></asp:Label>
        </td>
    </tr>
    <tr>
        <td style="background-color:White" colspan="4">
            <asp:Label ID="Label7" runat="server" Text="." ForeColor="White"></asp:Label>
        </td>
    </tr>
</table>  
           </ItemTemplate>
           <EditItemTemplate>
           <table>
    <tr>
        <td rowspan="4" style="background-color:black">
            <asp:Image ID="flightImg" runat="server" Height="80" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"Pic","Pics/{0}") %>' />
        </td>
        <td>
            <asp:Label ID="txt_id" runat="server" Text="Flight ID" Font-Bold="true" Font-Underline="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label1" runat="server" Text=":" Font-Bold="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="lbl_id" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"FightID") %>'></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="txt_company" runat="server" Text="Company" Font-Bold="true" Font-Underline="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label2" runat="server" Text=":" Font-Bold="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="lbl_company" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Company") %>'></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="txt_sits" runat="server" Text="Available Sits" Font-Bold="true" Font-Underline="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label3" runat="server" Text=":" Font-Bold="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="lbl_sits" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Sits") %>'></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="txt_quan" runat="server" Text="Outlet" Font-Bold="true" Font-Underline="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label4" runat="server" Text=":" Font-Bold="true"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="quan" runat="server" Text="1" ontextchanged="quan_TextChanged"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td></td>
        <td>
            <asp:Label ID="txt_price" runat="server" Text="Total Price" Font-Bold="true" Font-Underline="true"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label9" runat="server" Text=":" Font-Bold="true"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="total_price" runat="server" Enabled="false" Text='<%# DataBinder.Eval(Container.DataItem,"Price") %>'></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td></td>
        <td>
            <asp:Button ID="btn_confirm" runat="server" Text="Confirm" CommandName="Update" />
        </td>
        <td>
            <asp:Button ID="btn_cancel" runat="server" Text="Cancel" CommandName="Cancel" />
        </td>
    </tr>
</table>
           </EditItemTemplate>
       </asp:DataList>

Upvotes: 0

Views: 2017

Answers (1)

Noonit
Noonit

Reputation: 11

I've added a function "on text changed" to the first text box :

protected void quan_TextChanged(object sender, EventArgs e)
{
    TextBox t = (TextBox)sender;
    DataListItem item = (DataListItem)t.NamingContainer;
    int amount = int.Parse(((TextBox)item.FindControl("quan")).Text);
    DataTable dt = se.GetAllFlights();
    string price = dt.Rows[item.ItemIndex][4].ToString();
    int pos = price.IndexOf("$");
    price = ((int.Parse(price.Substring(0, pos))) * (int.Parse(t.Text))).ToString() + "$";
    dt.Rows[item.ItemIndex][4] = price.ToString();
    dlOrders.DataSource = dt;
    dlOrders.DataBind();        

}

instead of serching for a way to change the text in the second textbox, I've changed the value in the datatable associated with that textbox.

Upvotes: 1

Related Questions