我正在尝试使用绑定到 GridView 中的单元格的下拉列表来调用 SQL UPDATE 语句来更改 GridView 本身中的记录。下面是代码:
<asp:TemplateField HeaderText="Send To...">
<ItemTemplate>
<asp:DropDownList ID="StatusDD" runat="server" AutoPostBack="false" OnSelectedIndexChanged="StatusDD_SelectedIndexChanged">
<asp:ListItem Value="A">Active</asp:ListItem>
<asp:ListItem Value="C">Complete</asp:ListItem>
<asp:ListItem Value="I">In Process</asp:ListItem>
<asp:ListItem Value="E">Error</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
这是后面的代码,这是我的 try-catch 块在 ExecuteNonQuery() 处挂起我的页面的地方。 这是如何进行更新的正确语法吗?如您在上面看到的,我禁用了 AutoPostBack,但无论如何这对我没有任何作用:
protected void StatusDD_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
ToroGeneral toro = new ToroGeneral();
string connString = toro.GetOracle1ConnectionString();
using (OracleConnection conn1 = new OracleConnection(connString))
{
foreach (GridViewRow row in MMRGrid.Rows)
{
Control ctrl = row.FindControl("StatusDD") as DropDownList;
if (ctrl != null)
{
DropDownList ddl1 = (DropDownList)ctrl;
string qString = "";
// PreQuery work
DropDownList temp = new DropDownList();
temp = (DropDownList)row.FindControl("StatusDD");
string uKey = row.Cells[12].Text;
string tempStr = temp.SelectedValue.ToString().Trim();
if (ddl1.SelectedValue == "A")
{
qString = "UPDATE MATERIALMOVEREQUEST SET PROCESS_FLAG = 'A' WHERE UNIQUEKEY = '" + uKey + "'";
}
else if (ddl1.SelectedValue == "E")
{
qString = "UPDATE MATERIALMOVEREQUEST SET PROCESS_FLAG = 'E' WHERE UNIQUEKEY = '" + uKey + "'";
}
else if (ddl1.SelectedValue == "I")
{
qString = "UPDATE MATERIALMOVEREQUEST SET PROCESS_FLAG = 'I' WHERE UNIQUEKEY = '" + uKey + "'";
}
else if (ddl1.SelectedValue == "C")
{
qString = "UPDATE MATERIALMOVEREQUEST SET PROCESS_FLAG = 'C' WHERE UNIQUEKEY = '" + uKey + "'";
}
else
{
// Error
}
// Change the value of the record in the database.
conn1.Open();
OracleCommand cmd = conn1.CreateCommand();
OracleTransaction myTrans;
cmd.CommandText = qString;
myTrans = conn1.BeginTransaction(IsolationLevel.ReadCommitted);
cmd.Transaction = myTrans;
if (cmd.Connection.State == ConnectionState.Closed)
{
cmd.Connection.Open();
}
try
{
cmd.ExecuteNonQuery();
myTrans.Commit();
}
catch
{
myTrans.Rollback();
}
} // if
} // foreach
}