OnRowUpdating不取值编辑模板文本框

时间:2013-12-14 10:18:29

标签: c# asp.net .net gridview webforms

我正在尝试在行更新中更新gridview中的值。但是,它拒绝接受文本框中写入的值。如果页面没有回发,我会填充gridview。救命啊!

GRIDVIEW:

  <asp:UpdatePanel runat="server" UpdateMode=Conditional><ContentTemplate>
    <asp:GridView runat="server" ID="searchGV" AutoGenerateColumns="False" GridLines="None"
        CssClass="mGrid" EmptyDataText="The Search Did Not Return Any Results"
        PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" Width="100%" OnRowCancelingEdit="searchGV_RowCancelingEdit"
        OnRowEditing="searchGV_RowEditing" DataKeyNames="media_id" OnRowUpdating="searchGV_RowUpdating">
        <Columns>
            <asp:BoundField DataField="media_id" Visible="false" HeaderText="" />
            <asp:BoundField DataField="dir_path" Visible="false" HeaderText="Dir" />
            <asp:TemplateField HeaderText="Date Taken" >
                <ItemTemplate>
                    <asp:LinkButton ID="dateLinkCsBtn" OnClientClick='<%#Eval("dir_path","Javascript:return test(\"{0}\",event);")%>'
                        runat="server" Text='<%# Bind("date") %>'></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Media Type">
                <ItemTemplate>
                    <asp:Label runat="server" ID="mediaTypeLbl" Text='<%#Eval("description") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Alias File Name">
                <ItemTemplate>
                    <asp:Label runat="server" ID="aliasFileName" Text='<%#Bind("alias_file_name") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" Text='<%#Bind("alias_file_name") %>' ID="updateAliasTxt"></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="True" />
        </Columns>
    </asp:GridView>
    </ContentTemplate></asp:UpdatePanel>

背后的代码:

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        FillGrid();
    }

}

protected void searchGV_RowEditing(object sender, GridViewEditEventArgs e)
{
    searchGV.EditIndex = e.NewEditIndex;
    FillGrid();
}
protected void searchGV_RowCancelingEdit(object sender,
                          GridViewCancelEditEventArgs e)
{
    searchGV.EditIndex = -1;
    FillGrid();
}

public void FillGrid()
{
    Media_DAO media_query = new Media_DAO();
    searchGV.DataSource = media_query.get_search_media(alias_file_name_txt.Text.Trim(), Convert.ToInt16(mediaTypeDDL.SelectedValue),
            from_date_txt.Text.Trim(), to_date_txt.Text.Trim(), Convert.ToInt16(Session["user_id"]));
    searchGV.DataBind();
}

protected void searchGV_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

    GridViewRow row = searchGV.Rows[e.RowIndex];
    TextBox new_alias = (TextBox)searchGV.Rows[e.RowIndex].FindControl("updateAliasTxt");
    int media_id = Convert.ToInt16(searchGV.DataKeys[e.RowIndex]["media_id"]);

    Media_DAO media_query2 = new Media_DAO();
    media_query2.update_alias_name_by_id(media_id, new_alias.Text.Trim(), Convert.ToInt16(Session["user_id"]));
    searchGV.EditIndex = -1;
    FillGrid();
}

2 个答案:

答案 0 :(得分:1)

在声明网格时,您只指定了 1 * 数据键值 *

asDataKeyNames="media_id"

网格视图的DateKeys以从0开始的数组格式存储。 错误可能在于更新时获取 media_id 。您需要按如下方式进行更改。

protected void searchGV_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

    GridViewRow row = searchGV.Rows[e.RowIndex];
    TextBox new_alias = (TextBox)searchGV.Rows[e.RowIndex].FindControl("updateAliasTxt");
    int media_id = Convert.ToInt16(e.Keys[0]); //this will get the media_id

    Media_DAO media_query2 = new Media_DAO();
    media_query2.update_alias_name_by_id(media_id, new_alias.Text.Trim(), Convert.ToInt16(Session["user_id"]));
    searchGV.EditIndex = -1;
    FillGrid();
}

由于早期方法为searchGV.DataKeys[e.RowIndex]["media_id"],如果您尝试编辑的行号大于1,则可能无法获取media_id值。

答案 1 :(得分:1)

GridView的DataKeys属性返回DataKey个对象的集合,表示GridView每行的数据键值。因此,DataKeys[1]给出第2行,DataKeys[4]给出第5行......

因此,您需要使用ValueValues属性来访问关键字段的值。

而不是这样做:

int media_id = Convert.ToInt16(searchGV.DataKeys[e.RowIndex]["media_id"]);

这样做:

int media_id = Convert.ToInt16(searchGV.DataKeys[e.RowIndex].Values["media_id"]);
// You can also use the Value property which gives the key value at index 0 
int media_id = Convert.ToInt16(searchGV.DataKeys[e.RowIndex].Value);