简体   繁体   English

在网格视图控件中查找控件

[英]Find control in grid view control

I know there are alot of questions&answers about this but non of them works for me 我知道有很多与此相关的问题和答案,但都不是对我有用

Trying to populate this Drop Down List via a function in code behind: 尝试通过后面的代码中的函数填充此下拉列表:

This drop down list is for Images names, when a user choose one and clicks the "Update" link button in the Grid View I need to insert a suffix to the image name (for the path), so if the user choose an image name from the list the update button it will actually update the fileName + insert at the beginning the path, so it would be 此下拉列表用于图像名称,当用户选择一个图像名称并单击“网格”视图中的“更新”链接按钮时,我需要在图像名称后插入一个后缀(用于路径),因此如果用户选择一个图像名称从列表中的更新按钮,它将实际上更新fileName +在路径的开始处插入,因此

../images/gary.jpg ../images/gary.jpg

for the name gary. 为名字加里。

aspx page aspx页面

 <asp:TemplateField>
                <EditItemTemplate>                      
                        <asp:DropDownList ID="ddlImages" runat="server">
                        <Items>
                            <asp:ListItem Text="Choose" Value="" />
                        </Items>
                    </asp:DropDownList>
                </EditItemTemplate>
                ...

code behind 背后的代码

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlImages = (e.Row.FindControl("ddlImages") as DropDownList);
            ShowImages(ddlImages);

        }
}

"ddlImages" is null, how can I find him? “ ddlImages”为空,如何找到他?


Managed to find the drop down list like that: 设法找到这样的下拉列表:

if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList ddlImages = (DropDownList)e.Row.FindControl("ddlImages");
                ddlImages.DataSource = GetImages();
                ddlImages.DataBind();
            }
        }

Now i am facing another problem, in the aspx i have an "UpdateCommand" which take parameters and try to update the database. 现在我面临另一个问题,在aspx中,我有一个“ UpdateCommand”,它带有参数并尝试更新数据库。 The problem- it need to take the dropDownList chosen value but instead it take: "" how can i change it to take the value that the user choose in the dropDownList 问题-它需要采用dropDownList选定的值,但是要采用:“”我如何更改它以采用用户在dropDownList中选择的值

aspx page aspx页面

    UpdateCommand="UPDATE [investigator] SET [name] = @name, [area] = @area, [country] = @country,     [picture] = @picture, [review] = @review WHERE [Id] = @Id">

 <UpdateParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="area" Type="String" />
            <asp:Parameter Name="country" Type="String" />
            <asp:Parameter Name="picture" Type="String" />
            <asp:Parameter Name="review" Type="String" />
            <asp:Parameter Name="Id" Type="Int32" />
        </UpdateParameters>

Meaning- I have to change 意思是-我必须改变

<asp:Parameter Name="picture" Type="String" />

to drop down list selected value. 下拉列表中的选定值。 How can I achieve that? 我该如何实现?

Regarding your question about the update Parameter, you should use the ControlParameter instead of general Parameter. 关于更新参数的问题,应使用ControlParameter而不是常规参数。 Try replacing the Parameter Definition for the Dropdown list with following code. 尝试用以下代码替换下拉列表的参数定义。

<asp:ControlParameter Name="picture" ControlID="ddlImages" 
    PropertyName="SelectedValue" Type="String" />

UPDATE What you can do as alternate is, 更新您可以做的替代方法是,

Change the parameter back to 将参数更改回

<asp:Parameter Name="picture" Type="String" />

Add RowUpdating event handler to the gridview In the RowUpdating command set the data source parameter based on the selected value as below 将RowUpdating事件处理程序添加到gridview在RowUpdating命令中,根据选定的值设置数据源参数,如下所示

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

GridViewRow row = GridView1.Rows[e.RowIndex];
DropDownList ddlImages=  row.FindControl("ddlImages") as DropDownList;
SqlDataSource1.UpdateParameters["picture"].DefaultValue = ddlImages.SelectedValue;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM