我的目标是根据请求参数在打开FormView
中选择gridview中的行首先,我在Page_Load中获取了我的参数:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string param_id = Request["id"];
if (!string.IsNullOrEmpty(param_id))
{
SelectRowByDataKeyValue(param_id);
}
}
}
基于此tutorial我想在GridView中获取相应的行(称为gvPrincipal)并选择它:
public void SelectRowByDataKeyValue(string value)
{
gvPrincipal.DataBind();
int tmpSelectedIndex = 0;
int tmpPageIndex = 0;
for (int page = 0; page < gvPrincipal.PageCount; page++)
{
gvPrincipal.PageIndex = page;
gvPrincipal.DataBind();
for (int key = 0; key < gvPrincipal.DataKeys.Count; key++)
{
if (!string.IsNullOrEmpty(value) && value.Equals(Convert.ToString(gvPrincipal.DataKeys[key].Value)))
{
tmpSelectedIndex = key;
tmpPageIndex = page;
break;
}
}
}
gvPrincipal.PageIndex = tmpPageIndex;
gvPrincipal.SelectedIndex = tmpSelectedIndex;
gvPrincipal.SelectRow(tmpSelectedIndex);
}
问题1:在此之后,找到值,gvPrincipal.SelectedIndex是正确的,但gvPrincipal.SelectedValue给了我大部分时间不同的值
protected void gvPrincipal_SelectedIndexChanged(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine(String.Format("Selected Index : {0}, SelectedValue : {1}", gvPrincipal.SelectedIndex, /*wrong value*/gvPrincipal.SelectedValue));
}
问题2:在GridView中,页面索引按预期更新,但不是行(应该着色)
此外,每行都有一个ImageButton:
<Columns>
...
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImgBtnDetail" runat="server"
CommandName="Select"
CausesValidation="False" ImageUrl="~/Images/icon_detail_16.gif"/>
</ItemTemplate>
</asp:TemplateField>
...
</Columns>
单击此按钮会引发gvPrincipal_RowCommand(),它将ReadView设置为ReadOnly模式并显示详细信息:
protected void gvPrincipal_RowCommand(object sender, GridViewCommandEventArgs e)
{
//displays infos
}
使用gvPrincipalValue更新FormView数据源:
<asp:ObjectDataSource ID="odsfvPrincipalDetail" runat="server" TypeName="crm.dal"
SelectMethod="SelectElementById">
<SelectParameters>
<asp:ControlParameter ControlID="gvPrincipal" Name="id" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
我想在SelectRowByDataKeyValue中以编程方式触发相同的行为; 这是我的尝试:
public void SelectRowByDataKeyValue(string value)
{
...
GridViewRow row = gvPrincipal.SelectedRow;
ImageButton ImgBtnDetail = (ImageButton)row.FindControl("ImgBtnDetail");
CommandEventArgs args = new CommandEventArgs("Select", null);
gvPrincipal_RowCommand(this, new GridViewCommandEventArgs(row, ImgBtnDetail, args));
}
不幸的是没有任何事情发生
未被选中的行是我最关心的问题,我怎样才能克服这个问题?
答案 0 :(得分:1)
以下是在C#中手动调用rowCommand的方法
MyGridView_RowCommand(MyGridView,new GridViewCommandEventArgs(imgButton,new CommandEventArgs(imgButton.CommandName,imgButton.CommandArgument)));
MyGridView是一个GridView。 imgButton是ImageButton,特别是位于MyGridView中的GridViewRow,我调用的是RowCommand。我在CheckBox上调用这个RowCommand,它也位于MyGridView中,但复选框通常无法正常处理为行命令中的图像按钮。
protected void MyCheckBoxInGrid_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = sender as CheckBox;
GridViewRow row = ((GridViewRow)chk.NamingContainer);
if (chk.Checked)
{
ImageButton imgButton = row.FindControl("ImgDetail") as ImageButton;
MyGridView_RowCommand(MyGridView, new GridViewCommandEventArgs(imgButton , new CommandEventArgs(imgButton .CommandName, imgButton .CommandArgument)));
}}
问候! Zahid Ashraf