如何更改/获取datalist中文本框内的文本

时间:2014-01-24 06:26:14

标签: c# asp.net textbox datalist

我需要

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

 all of them INSIDE a datalist.

(更具体:一个文本框获取项目数量,第二个文本框需要显示价格。如果一个单位花费20美元,那么当用户在金额文本框中键入“2”时它将显示40 $ in the price textbox)。

问题是 - 我不知道如何从一个文本框接收金额,也不改变第二个(价格)文本框的值。你能帮忙吗?

先生,所以很好! : - )

我已经在datalist中使用了ItemCommand - 问题是我不需要那里的按钮所以我不能调用那个commnad。

我还尝试在第一个文本框中添加“更改文本”功能,之后仍然不知道如何继续进行。

    <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>

1 个答案:

答案 0 :(得分:1)

我在第一个文本框中添加了“on changed changed”功能:

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();        

}

我改变了与该文本框关联的数据表中的值,而不是修改第二个文本框中的文本。