0

i have a gridview that i have created without datasource control and that has data from a database table and the gridview also has a link selection in one column. The select link is pointed to ActivityID (maybe it will be a problem?)

 <asp:GridView ID="gwActivity" runat="server" CssClass="gwActivity" AutoGenerateColumns="false" OnSelectedIndexChanged="gwActivity_SelectedIndexChanged">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkSelect" Text="Select" runat="server" CommandArgument='<%# Eval("ActivityID") %>' OnClick="lnkSelect_Click">
                </asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="ActivityID" HeaderText="ActivitID" />
        <asp:BoundField DataField="Activity" HeaderText="Activit" />
        <asp:BoundField DataField="ActivityRegisteredDate" HeaderText="ActivityRegisteredDate" />
        <asp:BoundField DataField="Responsible" HeaderText="Responsible" />
        <asp:BoundField DataField="Category" HeaderText="Category" />
        <asp:BoundField DataField="Change_Requestor" HeaderText="Change_Requestor" />
        <asp:BoundField DataField="Priority" HeaderText="Priority" />
        <asp:BoundField DataField="Size" HeaderText="Size" />
        <asp:BoundField DataField="Status" HeaderText="Status" />
        <asp:BoundField DataField="System" HeaderText="System" />
        <asp:BoundField DataField="Comment" HeaderText="Comment" />
    </Columns>
</asp:GridView>

enter image description here

I have created a OnClick event.

protected void lnkSelect_Click(object sender, EventArgs e)
{
    txtActivity.Text = GridView1.SelectedRow.Cells[2].Text;
    ddlChange_Requestor.selectedvalue = GridView1.SelectedRow.Cells[6].selectevvalue;
}

i´m i missing something? should i maybe "FindControl".. Im a bit lost here?

e.g.

enter image description here

Activity textbox (txtActivity) = Test2 (it should say that in textbox)................................................... Change requestor dropdown (ddlChange_Requestor) = ... (find change request value and change dropdownlist)

2 Answers 2

1

You should switch to the GridView RowCommand.

<asp:GridView ID="gwActivity" runat="server" OnRowCommand="gwActivity_RowCommand">

And change the LinkButton to

<asp:LinkButton ID="lnkSelect" runat="server" CommandArgument='<%# Eval("ActivityID") %>'>Select</asp:LinkButton>

Now you can get all the data you need in the method.

protected void gwActivity_RowCommand(object sender, GridViewCommandEventArgs e)
{
    GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

    txtActivity.Text = row.Cells[2].Text;
    ddlChange_Requestor.SelectedValue = e.CommandArgument.ToString();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, Thank you this almost works. I got it to work for the textbox but not for the dropdownlist? should i not say ddlChange_Requestor.SelectedValue = what row/column in the gridview to use?
Check the values in the DDL. The CommandArgument of the LinkButton in the GridView should be one of those values.
I got it to work: ddlchange_requestor.SelectedValue = row.Cells[6].Text;
0

If you're just copying data from your datagrid row to the input fields, then what you're trying to do incurs a heavy execution cost. Handling the LinkButton OnClick event on the server side requires that the page be posted back, all the page events to fire (e.g. events to bind data to the template, execute the LinkButton OnClick event, and render the entire page into HTML), and the resulting HTML to be sent back to the browser for display.

If you can use javascript, you should consider using the OnClientClick event handler instead as this keeps you on the web page without posting back to the web server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.