如何使用GridView检查或突出显示ModalPopUoExtender中的预选选项? [英] How to check or highlight the pre-selected option in the ModalPopUoExtender with GridView?
问题描述
我正在使用Ajax ModalPopUpExtender控件更新GridView中的一列(状态列).一切正常.但是我现在想要的是:当我单击这些状态之一时,我希望打开Ajax ModalPopUp窗口,并在ModalPopUpExtender内部列出的选项中指定状态.例如,如果状态为已批准",并且ModalPopUpExtender中列出了四个选项,则应选中或突出显示已批准"选项. **那怎么办?**
ASP.NET代码:
I am using Ajax ModalPopUpExtender control to update one column (Status column) in the GridView. Everything works fine. But what I want now is: When I click on one of these status, I want the Ajax ModalPopUp Window to be opened with specifiying the status in the options listed inside the ModalPopUpExtender. For example, if the status is Approved, and I have four options be listed in the ModalPopUpExtender, the Approved option should be checked or highlighted. **So how to do that?**
ASP.NET Code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID"
width="900px" CssClass="mGrid"
DataSourceID="SqlDataSource1"
OnRowDataBound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" CssClass="alt" />
<HeaderStyle Font-Bold = "True" ForeColor="Black" Height="20px"/>
<Columns>
<asp:BoundField DataField="ID" HeaderText="No." InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="DivisionShortcut" HeaderText="Division"
SortExpression="DivisionShortcut" />
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<%-- This to make status be opened and edited through the Ajax ModalPopUp Window --%>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkSuggestionStatus" Text=''<%#Eval("Status")%>''
OnClick="lnkSuggestionStatus_Click">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<%--<asp:HyperLinkField HeaderText="Status"
SortExpression="Status" />--%>
</Columns>
<RowStyle HorizontalAlign="Center" />
</asp:GridView>
<asp:Button runat="server" ID="btnModalPopUp" style="display:none" />
<AjaxToolkit:ModalPopUpExtender ID="modalPopUpExtender1"
runat="server"
TargetControlID="btnModalPopUp"
PopupControlID="pnlPopUp"
BackgroundCssClass="popUpStyle"
PopupDragHandleControlID="panelDragHandle"
OkControlID="OKButton">
</AjaxToolkit:ModalPopUpExtender>
<asp:HiddenField ID="HiddenField1" runat="server"/>
<asp:Panel runat="server" ID="pnlPopUp" CssClass="popUpStyle">
<asp:RadioButtonList ID="StatusList" runat="server" RepeatColumns="1" RepeatDirection="Vertical"
RepeatLayout="Table" TextAlign="Right" DataSourceID="SuggestionStatusDataSource"
DataTextField="Status" DataValueField="ID">
<asp:ListItem id="option1" runat="server" Value="ACTIONED" />
<asp:ListItem id="option2" runat="server" Value="APPROVED" />
<asp:ListItem id="option3" runat="server" Value="PENDING" />
<asp:ListItem id="option4" runat="server" Value="TRANSFERRED" />
</asp:RadioButtonList>
<asp:SqlDataSource ID="SuggestionStatusDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource>
<asp:Button ID="confirmButton" runat="server" Text="Confirm"
OnClientClick="javascript:return confirm(''Are you sure you want to send an email notification about the safety suggestion to the owner?'')"
OnClick="btnSendStatus_Click" />
<asp:Button ID="OKButton" runat="server" Text="Close" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
C#代码隐藏:
C# code-behind:
protected void lnkSuggestionStatus_Click(object sender, EventArgs e)
{
LinkButton lnkSuggestionStatus = sender as LinkButton;
//var safetySuggestionsId =
//get reference to the row selected
GridViewRow gvrow = (GridViewRow)lnkSuggestionStatus.NamingContainer;
//set the selected index to the selected row so that the selected row will be highlighted
GridView1.SelectedIndex = gvrow.RowIndex;
//This HiddenField used to store the value of the ID
HiddenField1.Value = GridView1.DataKeys[gvrow.RowIndex].Value.ToString();
//ViewState["Username"] = gvrow.Cells[4].Text;
//show the modalPopUp
modalPopUpExtender1.Show();
}
public void btnSendStatus_Click(object sender, EventArgs e) {
//get the ID of the selected suggestion/row
var statusID = StatusList.SelectedValue;
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
//For updating the status of the safety suggestion
string updateCommand = "UPDATE SafetySuggestionsLog SET StatusID= @statusID where ID=@SafetySuggestionsID";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
{
cmd.Parameters.AddWithValue("@statusID", Convert.ToInt32(statusID));
cmd.Parameters.AddWithValue("@SafetySuggestionsID", Convert.ToInt32(HiddenField1.Value));
cmd.ExecuteNonQuery();
}
//reset the value of hiddenfield
HiddenField1.Value = "-1";
}
GridView1.DataBind();
SendSuggestionStatusToUser(statusID);
}
推荐答案
ConnectionStrings:testConnectionString%> SelectCommand ="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource> < asp:Button ID ="confirmButton" runat =服务器" Text =确认" OnClientClick ="javascript:return Confirm(``您确定要向所有者发送有关安全建议的电子邮件通知吗?") OnClick ="btnSendStatus_Click"/> < asp:Button ID ="OKButton" runat =服务器" Text =关闭"/> </asp:Panel> </ContentTemplate> </asp:UpdatePanel>
ConnectionStrings:testConnectionString %>" SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource> <asp:Button ID="confirmButton" runat="server" Text="Confirm" OnClientClick="javascript:return confirm(''Are you sure you want to send an email notification about the safety suggestion to the owner?'')" OnClick="btnSendStatus_Click" /> <asp:Button ID="OKButton" runat="server" Text="Close" /> </asp:Panel> </ContentTemplate> </asp:UpdatePanel>
C#代码隐藏:
C# code-behind:
protected void lnkSuggestionStatus_Click(object sender, EventArgs e)
{
LinkButton lnkSuggestionStatus = sender as LinkButton;
//var safetySuggestionsId =
//get reference to the row selected
GridViewRow gvrow = (GridViewRow)lnkSuggestionStatus.NamingContainer;
//set the selected index to the selected row so that the selected row will be highlighted
GridView1.SelectedIndex = gvrow.RowIndex;
//This HiddenField used to store the value of the ID
HiddenField1.Value = GridView1.DataKeys[gvrow.RowIndex].Value.ToString();
//ViewState["Username"] = gvrow.Cells[4].Text;
//show the modalPopUp
modalPopUpExtender1.Show();
}
public void btnSendStatus_Click(object sender, EventArgs e) {
//get the ID of the selected suggestion/row
var statusID = StatusList.SelectedValue;
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
//For updating the status of the safety suggestion
string updateCommand = "UPDATE SafetySuggestionsLog SET StatusID= @statusID where ID=@SafetySuggestionsID";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
{
cmd.Parameters.AddWithValue("@statusID", Convert.ToInt32(statusID));
cmd.Parameters.AddWithValue("@SafetySuggestionsID", Convert.ToInt32(HiddenField1.Value));
cmd.ExecuteNonQuery();
}
//reset the value of hiddenfield
HiddenField1.Value = "-1";
}
GridView1.DataBind();
SendSuggestionStatusToUser(statusID);
}
您需要这样做..
首先生成行数据绑定事件,该事件将为您选中复选框/单选按钮提供便利,当您将数据绑定到网格时,该按钮会自动检查到上一个条目.
单选按钮已选中的问题用于绑定行数据 [ bind-radio-button-list-to-column-in-gridview [ ^ ]
要突出显示您的单选按钮,可以使用css..
You need to go like this..
first of generate Row Data Bound event, which will tack care for your check-box/radio button which is checked automatically to previous entry while your bind the data to your grid.
Radio button checked issue for row data bound[^]
bind-radio-button-list-to-column-in-gridview[^]
for highlighting your radio button you can use css for it..
input[type=radio]:checked {
border: 1px solid black;
color: red;
}
它会突出显示选中或选中的单选按钮.
it will highlight your radio button which is selected or checked.
这篇关于如何使用GridView检查或突出显示ModalPopUoExtender中的预选选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!