如何在没有主键的数据库表上执行GridView更新 [英] How to perform GridView Update on database table with No Primary Key

查看:82
本文介绍了如何在没有主键的数据库表上执行GridView更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在没有主键的数据库表上执行GridView更新?



我编写了一个onupdate命令,但它没有触发。这是我的代码:



.aspx

How do I perform a GridView Update on a database table which has no primary key?

I have coded an onupdatecommand but it is not firing. Here are my codes:

.aspx

<asp:ScriptManager ID="ScriptManager" runat="server" />
          MSG_ID:  <asp:TextBox ID="txtsearchMSGID" runat="server" OnTextChanged="txtsearchMSGID_TextChanged"></asp:TextBox>
          <asp:Button ID="clearButton" runat="server" Text="Clear" Width="75px" />
                <asp:Label ID="Message" runat="server" Text="Message"></asp:Label>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>

                        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                            OnRowUpdated="GridView1_RowUpdated"
                            OnRowCancelingEdit="GridView1_RowCancelingEdit"
                            OnRowEditing="GridView1_RowEditing"  BackColor="White" 
                            BorderColor="#999999" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" GridLines="Vertical" AutoGenerateColumns="False" DataKeyNames="MSG_ID">
                            
                            <AlternatingRowStyle BackColor="#DCDCDC" />
                            <Columns>
                                <asp:CommandField ShowEditButton="True" />
                                <asp:BoundField DataField="MSG_ID" HeaderText="MSG_ID" SortExpression="MSG_ID" />
                                <asp:BoundField DataField="ACTION_IND" HeaderText="ACTION_IND" SortExpression="ACTION_IND" />
                                <asp:BoundField DataField="ACTION_DATE" HeaderText="ACTION_DATE" SortExpression="ACTION_DATE" />
                                <asp:BoundField DataField="USERNAME" HeaderText="USERNAME" SortExpression="USERNAME" />
                            <asp:CommandField ShowInsertButton="false" ShowEditButton="true" ShowDeleteButton="false" />
                            </Columns>
                            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                            <SortedAscendingCellStyle BackColor="#F1F1F1" />
                            <SortedAscendingHeaderStyle BackColor="#0000A9" />
                            <SortedDescendingCellStyle BackColor="#CAC9C9" />
                            <SortedDescendingHeaderStyle BackColor="#000065" />
                        </asp:GridView>
                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:gsiadmin %>" ProviderName="<%$ ConnectionStrings:gsiadmin.ProviderName %>"
                            SelectCommand="SELECT * FROM GSI_SWIFT_AUDIT"
                            UpdateCommand="UPDATE GSI_SWIFT_AUDIT SET [ACTION_IND]=@ACTION_IND, [ACTION_DATE]=@ACTION_DATE, [USERNAME]=@USERNAME WHERE ([MSG_ID]=@MSG_ID)"
                            FilterExpression="MSG_ID = {0}">
                            <FilterParameters>
                                <asp:ControlParameter Name="txtsearchMSGID" ControlID="txtsearchMSGID" PropertyName="Text" />
                            </FilterParameters>
                            <UpdateParameters>
                                <asp:Parameter Name="MSG_ID" Type="Decimal" />
                                <asp:Parameter Name="ACTION_IND" Type="String" />
                                <asp:Parameter Name="ACTION_DATE" Type="DateTime" />
                                <asp:Parameter Name="USERNAME" Type="String" />
                            </UpdateParameters>
                            <InsertParameters>
                                <asp:Parameter Name="MSG_ID" Type="Decimal" />
                                <asp:Parameter Name="ACTION_IND" Type="String" />
                                <asp:Parameter Name="ACTION_DATE" Type="DateTime" />
                                <asp:Parameter Name="USERNAME" Type="String" />
                            </InsertParameters>
                        </asp:SqlDataSource>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="txtsearchMSGID" EventName="TextChanged" />
                    </Triggers>
                </asp:UpdatePanel>





.aspx.cs



.aspx.cs

public partial class Approve_Swift : System.Web.UI.Page
{
    string MSGID = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        txtsearchMSGID.Attributes.Add("onkeyup", "setTimeout('__doPostBack(\\'" + txtsearchMSGID.ClientID.Replace("_", "$") + "\\',\\'\\')', 0);");
        if (!IsPostBack)
        {
            GridView1.DataBind();
        }
    }

    protected void Clear_Click(object sender, EventArgs e)
    {
        txtsearchMSGID.Text = "";
    }



    protected void txtsearchMSGID_TextChanged(object sender, EventArgs e)
    {
        MSGID = txtsearchMSGID.Text;

    }

    protected void GridView1_RowUpdated(Object sender, GridViewUpdatedEventArgs e)
    {

        // Indicate whether the update operation succeeded.
        if (e.Exception == null)
        {
            Message.Text = "Row updated successfully.";
        }
        else
        {
            e.ExceptionHandled = true;
            Message.Text = "An error occurred while attempting to update the row.";
        }

    }

    protected void GridView1_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
    {

        // The update operation was canceled. Clear the message label.
        Message.Text = "";

    }

    protected void GridView1_RowEditing(Object sender, GridViewEditEventArgs e)
    {
        // The GridView control is entering edit mode. Clear the message label.
        GridView1.EditIndex = e.NewEditIndex;
        DataBind();
        Message.Text = "";
    }
}











推荐答案

ConnectionStrings:gsiadmin%> ProviderName = <%
ConnectionStrings:gsiadmin %>" ProviderName="<%


ConnectionStrings:gsiadmin.ProviderName%>
SelectCommand = SELECT * FROM GSI_SWIFT_AUDIT
UpdateCommand = UPDATE GSI_SWIFT_AUDIT SET [ACTION_IND] = @ ACTION_IND,[ACTION_DATE] = @ ACTION_DATE,[USERNAME] = @ USERNAME WHERE([MSG_ID] = @ MSG_ID)
FilterExpression = MSG_ID = {0} >
< FilterParameters>
< as p:ControlParameter Name = txtsearchMSGID ControlID = txtsearchMSGID PropertyName = Text />
< / FilterParameters >
< UpdateParameters>
< asp:参数名称= MSG_ID Type = 十进制 />
< asp:参数名称= ACTION_IND Type = String />
< asp:参数名称= ACTION_DATE Type = DateTime />
< asp:参数名称= USERNAME Type = String />
< / UpdateParameters >
< InsertParameters>
< asp:参数名称= MSG_ID Type = 十进制 />
< asp:参数名称= ACTION_IND Type = String />
< asp:参数名称= ACTION_DATE Type = DateTime />
< asp:参数名称= USERNAME Type = String />
< / InsertParameters >
< / asp:SqlDataSource >
< / ContentTemplate >
<触发器>
< asp:AsyncPostBackTrigger ControlID = txtsearchMSGID EventName = TextChanged />
< / 触发器 >
< / asp:UpdatePanel >
ConnectionStrings:gsiadmin.ProviderName %>" SelectCommand="SELECT * FROM GSI_SWIFT_AUDIT" UpdateCommand="UPDATE GSI_SWIFT_AUDIT SET [ACTION_IND]=@ACTION_IND, [ACTION_DATE]=@ACTION_DATE, [USERNAME]=@USERNAME WHERE ([MSG_ID]=@MSG_ID)" FilterExpression="MSG_ID = {0}"> <FilterParameters> <asp:ControlParameter Name="txtsearchMSGID" ControlID="txtsearchMSGID" PropertyName="Text" /> </FilterParameters> <UpdateParameters> <asp:Parameter Name="MSG_ID" Type="Decimal" /> <asp:Parameter Name="ACTION_IND" Type="String" /> <asp:Parameter Name="ACTION_DATE" Type="DateTime" /> <asp:Parameter Name="USERNAME" Type="String" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="MSG_ID" Type="Decimal" /> <asp:Parameter Name="ACTION_IND" Type="String" /> <asp:Parameter Name="ACTION_DATE" Type="DateTime" /> <asp:Parameter Name="USERNAME" Type="String" /> </InsertParameters> </asp:SqlDataSource> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="txtsearchMSGID" EventName="TextChanged" /> </Triggers> </asp:UpdatePanel>





.aspx.cs



.aspx.cs

public partial class Approve_Swift : System.Web.UI.Page
{
    string MSGID = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        txtsearchMSGID.Attributes.Add("onkeyup", "setTimeout('__doPostBack(\\'" + txtsearchMSGID.ClientID.Replace("_", "


)+ \\',\\'\\')',0););
if (!IsPostBack)
{
GridView1.DataBind();
}
}

受保护 void Clear_Click( object sender,EventArgs e)
{
txtsearchMSGID.Text = ;
}



受保护 void txtsearchMSGID_TextChanged( object sender,EventArgs e)
{
MSGID = txtsearchMSGID.Text;

}

protected void GridView1_RowUpdated(< span class =code-sdkkeyword> Object sender,GridViewUpdatedEventArgs e)
{

// 指示更新操作是否成功。
if (e.Exception == null
{
Message.Text = 行更新成功
}
else
{
e.ExceptionHandled = true ;
Message.Text = 尝试更新行时发生错误。;
}

}

受保护 void GridView1_RowCancelingEdit( Object sender,GridViewCancelEditEventArgs e)
{

// 更新操作已取消。清除消息标签。
Message.Text = ;

}

受保护 void GridView1_RowEditing(< span class =code-sdkkeyword> Object
sender,GridViewEditEventArgs e)
{
// GridView控件正在进入编辑模式。清除消息标签。
GridView1.EditIndex = e.NewEditIndex;
DataBind();
Message.Text = ;
}
}
") + "\\',\\'\\')', 0);"); if (!IsPostBack) { GridView1.DataBind(); } } protected void Clear_Click(object sender, EventArgs e) { txtsearchMSGID.Text = ""; } protected void txtsearchMSGID_TextChanged(object sender, EventArgs e) { MSGID = txtsearchMSGID.Text; } protected void GridView1_RowUpdated(Object sender, GridViewUpdatedEventArgs e) { // Indicate whether the update operation succeeded. if (e.Exception == null) { Message.Text = "Row updated successfully."; } else { e.ExceptionHandled = true; Message.Text = "An error occurred while attempting to update the row."; } } protected void GridView1_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e) { // The update operation was canceled. Clear the message label. Message.Text = ""; } protected void GridView1_RowEditing(Object sender, GridViewEditEventArgs e) { // The GridView control is entering edit mode. Clear the message label. GridView1.EditIndex = e.NewEditIndex; DataBind(); Message.Text = ""; } }












这篇关于如何在没有主键的数据库表上执行GridView更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆