Cute Bear
Cute Bear

Reputation: 3291

How to call the appropriate parameter on

This might be very basic question to some but I am struggling for quite some time.

I created an aspx page which has a search ability on User Table (Database). When Search button is cliked related Grid is filled with UserNames. And I put an Edit HyperLink in every row right next to UserName Column.

However Edit button should get a parameter named "UserCode" (this information also comes when I retrieved the names) but I just couldn't write the appropriate code to the related line. Please in my codes there is a line contains I DONT KNOW WHAT TO TYPE IN HERE. Instead of index solution, any other suggestions are welcome.

    DataTable oDataTable;
    DataView oDataView;

    protected void SearchLinkButton_Click(object sender, EventArgs e)
    {
        MembershipUserCollection allUsers = Membership.GetAllUsers();
        if (allUsers != null)
        {
            oDataTable = new SystemUserAccountBsWrapper().SearchAllUsers(null); //Fetchs all user data
            oDataView = oDataTable.DefaultView;
            oDataView.Sort = "UserName";
            oDataView.RowFilter = "UserName LIKE '%" + UserNameTextBox.Text + "%'";
            UserGridView.DataSource = oDataView;
            UserGridView.DataBind();
        }
    }
    protected void UserGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink EditHyperLink = (HyperLink)e.Row.FindControl("EditHyperLink");
            EditHyperLink.NavigateUrl("EditUserInfo.aspx?key=" + oDataTable.Rows[**I DONT KNOW WHAT TO TYPE IN HERE**]["UserCode"]);
        }
    }

Sorry for my bad English.

Upvotes: 1

Views: 139

Answers (4)

Adrian Iftode
Adrian Iftode

Reputation: 15683

DataBinder.Eval can be used on codebehind also

EditHyperLink
      .NavigateUrl("EditUserInfo.aspx?key=" + DataBinder.Eval(e.Row.DataItem,"UserCode"),650, 500, true);

Upvotes: 2

SeanCocteau
SeanCocteau

Reputation: 1876

You can use the e.Row.DataItem to obtain the values you require, for example...

// If you require the first item in the row use...
e.Row.DataItem[0]

If I'm using a strongly typed Datarow / object, I prefer to cast that into its strongly typed form, i.e.

// For datarows
(MyTypedDataRow)((DataRowView)e.Row.DataItem.Row).MyPropertyName;

HTH

Upvotes: 0

Icarus
Icarus

Reputation: 63970

You need the DataIndex:

protected void UserGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HyperLink EditHyperLink = (HyperLink)e.Row.FindControl("EditHyperLink");
        EditHyperLink.NavigateUrl=("EditUserInfo.aspx?key=" + oDataTable.Rows[e.Row.DataItemIndex]["UserCode"], 650, 500, true);
    }
}

But there's a better way to do that:

<columns>
    <asp:HyperLinkColumn
                 HeaderText="Edit"
                 DataNavigateUrlField="UserCode"
                 DataNavigateUrlFormatString="EditUserInfo.aspx?key={0}"
                 Text="Edit"
                 Target="_blank"
                 />
 </columns>

Upvotes: 1

Dante
Dante

Reputation: 3891

e.Row gives you the current row, you shouldn't have to access the oDataTable.Rows by index.

So, that would be:

e.Row["UserCode"]

instead of:

oDataTable.Rows[**I DONT KNOW WHAT TO TYPE IN HERE**]["UserCode"]

Upvotes: 0

Related Questions