我已經動態創建了一個下拉串列,一旦頁面加載,一切都很好。在下拉選單旁邊的 gridview 的每一行中都有一個按鈕。此按鈕更新所選專案的表格。我得到了我的密鑰,但我得到的物件參考沒有設定為實體。這是因為下拉控制元件的 id 被埋在 ct100 東西中嗎?
Dim ddluid As String = CType(FindControl("Ddlos"), DropDownList).SelectedItem.Value
這是我從頁面源中刷過的:
<select name="ctl00$ContentPlaceHolder2$GridView1$ctl02$Ddlos" id="ctl00_ContentPlaceHolder2_GridView1_ctl02_Ddlos">
<option value="0">-Select-</option>
<option value="503841a3-c615-4e4d-8cf5-20fbddbf7a7f">John Doe</option>
<option value="9b38e9cd-f16f-4fdd-a82e-501c866f9e45">Sam Beacon</option>
<option value="fe6158c5-a549-443f-b5ff-c011937db1d7">John Doey</option>
<option value="295e9f85-6ea6-46ec-9c00-c38d64023517">Scot King</option>
Protected Sub btnsaveinfo(sender As Object, e As EventArgs)
Dim key As String = ""
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
key = GridView1.DataKeys(row.RowIndex).Value.ToString()
Dim ddluid As String = CType(FindControl("Ddlos"), DropDownList).SelectedItem.Value
'Dim idvalue As String = ddluid.SelectedValue
'Dim louid As Guid =
Dim cn As New SqlConnection(ConfigurationManager.ConnectionStrings("sqlConnectionString").ConnectionString)
Dim cmdupdate As New SqlCommand("update Profiles SET [loanrepid] = @loanrepid WHERE ApplicantID=@ApplicantID", cn)
cmdupdate.Parameters.AddWithValue("@loanrepid", "1")
cmdupdate.Parameters.AddWithValue("@ApplicantID", key)
cmdupdate.CommandType = System.Data.CommandType.Text
cmdupdate.Connection = cn
cn.Open()
cmdupdate.ExecuteNonQuery()
cn.Close()
End If
Next
lblresult.Text = " Details Updated Successfully"
lblresult.Visible = True
End Sub
<asp:TemplateField HeaderText="Loan Officer" SortExpression="Loan Officer" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList ID="Ddlos" runat="server" Visible="false"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
uj5u.com熱心網友回復:
幾件事。
您可以讓用戶進行所有更改,在網格下方,一個保存按鈕。
但是,既然您在每一行上都有一個按鈕,可以將該行保存回資料庫嗎?
那么不需要回圈一個保存按鈕行中的所有行????
但是,如果用戶更改了幾行會發生什么 - 忘記點擊保存,然后點擊保存?換句話說,用戶在這里會有些困惑嗎?
那么,不清楚點擊保存按鈕應該只保存當前行嗎?
如果這是目標,那么我們可以說這個設定:
<asp:GridView ID="GHotels" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table" Width="50%">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="Rate">
<ItemTemplate>
<asp:DropDownList ID="cboRank" runat="server"
DataValueField="ID"
DataTextField="Rating" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Save" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button ID="cmdSave" runat="server" Text="Save" CssClass="btn" Width="60"
OnClick="cmdSave_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
要加載的代碼是:
Dim rstRank As New DataTable ' to load cbo box
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadData()
End If
End Sub
Sub LoadData()
' load combo box data
rstRank = MyRst("SELECT ID, Rating from tblRating ORDER BY ID")
Dim strSQL As String =
"SELECT ID, FirstName, LastName, HotelName, Description, Ranking from tblHotels"
GHotels.DataSource = MyRst(strSQL)
GHotels.DataBind()
End Sub
Function MyRst(strSQL As String) As DataTable
Dim rst As New DataTable
Using conn As New SqlConnection(My.Settings.TEST4)
Using cmdSQL As New SqlCommand(strSQL, conn)
conn.Open()
rst.Load(cmdSQL.ExecuteReader)
End Using
End Using
Return rst
End Function
Protected Sub GHotels_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GHotels.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cboRank As DropDownList = e.Row.FindControl("cboRank")
cboRank.DataSource = rstRank
cboRank.DataBind()
' add blank row
cboRank.Items.Insert(0, New ListItem("Select", "0"))
Dim v As Object = e.Row.DataItem
Dim rData As DataRowView = e.Row.DataItem
If Not IsDBNull(rData("Ranking")) Then
cboRank.SelectedValue = rData("Ranking")
End If
End If
End Sub
我們現在有了這個:

好的,所以保存按鈕 - 保存一行?
這有效:
Protected Sub cmdSave_Click(sender As Object, e As EventArgs)
Dim btn As Button = sender
Dim gRow As GridViewRow = btn.NamingContainer
Dim cboRank As DropDownList = gRow.FindControl("cboRank")
Dim strSQL As String = "UPDATE tblHotels SET Ranking = @Rating WHERE ID = @ID"
Using conn As New SqlConnection(My.Settings.TEST4)
Using cmdSQL As New SqlCommand(strSQL, conn)
cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = GHotels.DataKeys(gRow.RowIndex).Value
cmdSQL.Parameters.Add("@Rating", SqlDbType.Int).Value = cboRank.SelectedItem.Value
conn.Open()
cmdSQL.ExecuteNonQuery()
End Using
End Using
End Sub
So, as you can see, no real need to loop and save ALL rows, is there?
I would think that if you want ONE save button?
Then move the save button OUT of the grid, and then save have this:
<asp:GridView ID="GHotels" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table" Width="50%">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="Rate">
<ItemTemplate>
<asp:DropDownList ID="cboRank" runat="server"
DataValueField="ID"
DataTextField="Rating" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="cmdSave" runat="server" Text="Save" CssClass="btn" Width="60"
OnClick="cmdSave_Click" />
And now this:

And the one save button (which I think after saving should say navagate to some other page - since we are done). That code to now save all rows, would be:
Protected Sub cmdSave_Click(sender As Object, e As EventArgs)
Using conn As New SqlConnection(My.Settings.TEST4)
Dim strSQL As String = "UPDATE tblHotels SET Ranking = @Rating WHERE ID = @ID"
conn.Open()
For Each gRow As GridViewRow In GHotels.Rows
Dim cboRank As DropDownList = gRow.FindControl("cboRank")
Using cmdSQL As New SqlCommand(strSQL, conn)
cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = GHotels.DataKeys(gRow.RowIndex).Value
cmdSQL.Parameters.Add("@Rating", SqlDbType.Int).Value = cboRank.SelectedItem.Value
cmdSQL.ExecuteNonQuery()
End Using
Next
End Using
End Sub
So, again the code is rather simular for saving "all" rows, or just the one row.
However, we could also drop in a button beside "save" called "un-do" or "cancel"
So, the above shows how to get the row click - for ONE row save, and the 2nd code shows how do to this for all rows.
uj5u.com熱心網友回復:
謝謝你對這個問題的建議。是的,我現在將它作為一個按鈕。我有幾件事導致它無法作業。我想只有一個按鈕來重新保存頁面上的選擇。
Protected Sub btnsaveinfo_Click(sender As Object, e As EventArgs) Handles btnsaveinfo.Click
Dim key As String = ""
For Each grow As GridViewRow In GridView1.Rows
If grow.RowType = DataControlRowType.DataRow Then
key = GridView1.DataKeys(grow.RowIndex).Value.ToString()
Dim ddlo As DropDownList = grow.FindControl("Ddlos")
Dim selectedlo As String = ddlo.SelectedValue
Dim result As Int16 = ddlo.SelectedIndex
If result = 0 Then
lblresult.Text = "You must select a Loan Officer for each loan"
lblresult.ForeColor = Drawing.Color.Red
lblresult.Visible = True
Exit Sub
End If
Dim loguid As Guid = Guid.Parse(selectedlo)
Dim ddproc As DropDownList = grow.FindControl("ddlprocessor")
Dim selectedproc As String = ddproc.SelectedValue
Dim cn As New SqlConnection(ConfigurationManager.ConnectionStrings("sqlConnectionString").ConnectionString)
Dim cmdupdate As New SqlCommand("update Loanapps SET [loanrepid] = @loanrepid WHERE ApplicantID=@ApplicantID", cn)
cmdupdate.Parameters.AddWithValue("@loanrepid", loguid)
cmdupdate.Parameters.AddWithValue("@ApplicantID", key)
cmdupdate.CommandType = System.Data.CommandType.Text
cmdupdate.Connection = cn
cn.Open()
cmdupdate.ExecuteNonQuery()
cn.Close()
If ddproc.SelectedIndex <> 0 Then
Dim procguid As Guid = Guid.Parse(selectedproc)
Dim cmdupdate2 As New SqlCommand("update Loanapps SET [processorid] = @processorid WHERE ApplicantID=@ApplicantID", cn)
cmdupdate2.Parameters.AddWithValue("@processorid", procguid)
cmdupdate2.Parameters.AddWithValue("@ApplicantID", key)
cmdupdate2.CommandType = System.Data.CommandType.Text
cmdupdate2.Connection = cn
cn.Open()
cmdupdate2.ExecuteNonQuery()
cn.Close()
End If
End If
Next
' Response.Redirect("default.aspx")
lblresult.Text = " Details Updated Successfully"
lblresult.ForeColor = Drawing.Color.Green
lblresult.Visible = True
Page.ClientScript.RegisterStartupScript(Me.GetType(), "alertscript", "<script>$('.TempAlert1').hide(8000);</script>")
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/409730.html
標籤:
