简体   繁体   English

如何使用列表数据源从ItemTemplate中的文本框更新GridView

[英]How to update GridView from TextBox in ItemTemplate with List DataSource

I'm stuck and have no idea what to do, can you help me please? 我被困住了,不知道该怎么办,请您能帮我吗?

I have this grid in page markup 我在页面标记中有此网格

<asp:GridView  ID="gridEmployees" runat="server" 
    AllowPaging="True"
    AllowSorting="<%# AllowSorting %>"
    OnPageIndexChanging="grdView_PageIndexChanging" 
    OnSorting="gridEmployees_Sorting" 
    OnRowEditing="gridEmployees_RowEditing"
    OnRowUpdating="gridEmployees_RowUpdating"
    OnRowCancelingEdit="gridEmployees_RowCancelingEdit"
    OnRowUpdated="gridEmployees_RowUpdated"
    AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="Full name" SortExpression="FullName">
            <ItemTemplate>
                <asp:Label runat="server" ID="lbSalary" Width="200px" Text='<%# Eval("FullName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Salary" SortExpression="Salary">
            <%--http://www.codeproject.com/Articles/23471/Editable-GridView-in-ASP-NET--%>
            <ItemTemplate >
                <div >
                    <asp:TextBox Width="100px" OnTextChanged="tbSalary_TextChanged" AutoPostBack="True" style="text-align: right" TextMode="SingleLine" runat="server" ID="tbSalary" Text='<%# Bind("Salary", "{0:c0}") %>'></asp:TextBox>
                </div>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

I want two columns - label (name) and textBox (salary). 我想要两列-标签(名称)和文本框(薪水)。 When user edits TextBox - data have to be updated automatically. 用户编辑TextBox时-数据必须自动更新。

My CodeBehind looks like: 我的CodeBehind看起来像:

    private static readonly DbManager DbManager = new DbManager();

    private int selectedJobId = 1; // TODO:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddlJobs.DataTextField = "name";
            ddlJobs.DataValueField = "id";
            ddlJobs.DataSource = DbManager.GetJobs();
            ddlJobs.SelectedValue = selectedJobId.ToString();
            ddlJobs.DataBind();

            ddlJobs.DataBound += (o, args) =>
                {
                    selectedJobId = Convert.ToInt32(ddlJobs.SelectedValue);
                    FillTable();
                };
        }
        else
        {
            selectedJobId = Convert.ToInt32(ddlJobs.SelectedValue);
        }
        Page.DataBind();
    }

    protected void FillTable()
    {
        gridEmployees.DataSource = GetEmployees(); // List<Employees>
        gridEmployees.DataBind();
    }

I thought that row will raise Updated event when I update TextBox, but, unfortunately, it doesnt, probaly it's 'cause grid is not in editMode. 我以为,当我更新TextBox时,该行将引发Updated事件,但不幸的是,事实并非如此,原因可能是因为网格不在editMode中。 So, I just made workaround - if TB edited - raise event and process it. 因此,我只是采取了变通方法-如果TB已编辑-引发事件并对其进行处理。

Well, now page is reloading with IsPostback = false, but no event is raised, not WebGrid's not TB's. 好了,现在页面正在使用IsPostback = false重新加载,但是没有引发任何事件,不是WebGrid的不是TB的。

I've found some clue here , but my source is not SqlDataSource. 我在这里找到了一些线索,但是我的来源不是SqlDataSource。 What's wrong with my code, source? 源代码有什么问题? How to update row when TB changed in my case. 在我的情况下,TB发生变化时如何更新行。

只需阅读这篇文章即可。您将能够从gridview内部的文本框中触发textchange事件[ http://www.codeproject.com/Tips/663684/Fire-TextBox-TextChanged-Event-from-GridView][1]

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

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