简体   繁体   English

如何更改/获取数据列表中文本框内的文本

[英]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). (更具体的说:一个文本框正在获取商品数量,第二个文本框需要显示价格。如果一个单位的价格为20美元,则当用户在数量文本框中输入“ 2”时,它将在商品价格栏中显示40美元价格文本框)。

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. 我已经在数据列表中使用了ItemCommand了-问题是我不需要在那里的按钮,因此无法调用该命令。

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>

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. 我已经更改了与该文本框相关联的数据表中的值,而不是使用寻找方法来更改第二个文本框中的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM