Frage

I have a gridview with some data from my DataBase and each line monitors something (in the real world). One of the columns tells me if monitoring is enabled or disabled (0 or 1). I have added a column (Templatefield) on the gridview with a LinkButton. This TemplateField is declared as follows :

<asp:TemplateField HeaderText="Action">
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton1" class="btn" runat="server" CausesValidation="false" 
            CommandName="Update" Text='<%# Eval("Disable").ToString() == "1" ? "Enable" : "Disable" %>'></asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

As you can see the property Text of the button is "Enable" (if the monitoring is currently disabled) or "Disable" (if the monitoring is currently enabled). That works fine.

The gridview is linked to an ObjectDataSource and the Linkbutton is linked to the command Update of said DataSource. The Update command of the ObjectDataSource calls a method from one of my DLLs and I need to pass the value (0 or 1) that will be written in the DataBase.

How shall I proceed in the Updating Event so that when I click this button it either Enables or Disables (sets the field to 0 or 1) in the DataBase depending on its current status.

My issue comes from the Updating Event on the ObjectDataSource. I can't seem to pass any parameters

protected void ods_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
    //I would like to accomplish something like
    e.InputParameters["value"] = isItEnabled ? 0 : 1;
}

Any ideas on how to proceed?

Edit 1 : My ods markup

<asp:ObjectDataSource ID="odsHistoriqueSurveillance" runat="server" 
    OldValuesParameterFormatString="original_{0}" 
    onselecting="odsHistoriqueSurveillance_Selecting" SelectMethod="getHistorique" 
    TypeName="WebOrderProcessingBLL.SRVCheckValueBLLWrapper" 
    onUpdating="ods_Updating"
    UpdateMethod="EnableOrDisableSurveillanceFromHistorique">
    <SelectParameters>
        <asp:Parameter Name="ParamName" Type="String" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="original_id" Type="Int32" />
        <asp:Parameter Name="value" Type="Byte" />
    </UpdateParameters>
</asp:ObjectDataSource>
War es hilfreich?

Lösung

Do a find of your LinkButton in the Updating event like below:

LinkButton lb = (LinkButton) GridView1.Rows[GridView1.EditIndex].FindControl("LinkButton1");
if(lb!=null){
  //Now check what is the Text of your link button. Depending on Enable or disable you set the parameter value as needed
  e.InputParameters["value"] = lb.Text=="Disable"? 0 : 1;
}

Andere Tipps

What I do in this situations is add a onClick event to the input element that you are clicking, and then adding all the values I need as attributes, something like this:

<asp:TemplateField HeaderText="Action">
   <ItemTemplate>
       <asp:LinkButton ID="LinkButton1" class="btn" runat="server" 
           CommandName="Update" Text='<%# Eval("Disable").ToString() == "1" ? "Enable" : "Disable" %>' OnClick="DoSomething" id='<%# Eval("RecordID")'></asp:LinkButton>
   </ItemTemplate>
</asp:TemplateField>

And on the codebehind:

protected void DoSomething(object sender, EventArgs e)
{
   var id = (sender as LinkButton).Attributes["RecordID"];
   var state = (sender as LinkButton).Text;
}

According to your ObjectDataSource markup, define a methods as

    public void EnableOrDisableSurveillanceFromHistorique(int origional_id, byte value)
    {
    }

For more details refer to Object Data Source Strongly Type Source

Also edit ODS markup as

    <UpdateParameters>
        <asp:ControlParameter ControlID="control" Name="origional_id" PropertyName="SelectedValue" Type="int" />
    <asp:ControlParameter ControlID="control" Name="value" PropertyName="SelectedValue" Type="byte" />
    </UpdateParameters>        
protected void ods_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
    if(e.CommandName=="Update")
     GridViewRow row = (GridViewRow)ods.Rows[e.RowIndex];
     LinkButton lnkBtn = (LinkButton)row.FindControl("LinkButton1");
    //I would like to accomplish something like
      // if (lnkBtn.Enabled == true)
         if (lnkBtn.Text == "Enabled")
           {
                //Do your stuff .
           }
//    e.InputParameters["value"] = isItEnabled ? 0 : 1;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top